• 签名和验证-Sign
    • 介绍
    • 使用

    签名和验证-Sign

    介绍

    Hutool针对java.security.Signature做了简化包装,包装类为:Sign,用于生成签名和签名验证。

    对于签名算法,Hutool封装了JDK的,具体介绍见:https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Signature:

    1. // The RSA signature algorithm
    2. NONEwithRSA
    3. // The MD2/MD5 with RSA Encryption signature algorithm
    4. MD2withRSA
    5. MD5withRSA
    6. // The signature algorithm with SHA-* and the RSA
    7. SHA1withRSA
    8. SHA256withRSA
    9. SHA384withRSA
    10. SHA512withRSA
    11. // The Digital Signature Algorithm
    12. NONEwithDSA
    13. // The DSA with SHA-1 signature algorithm
    14. SHA1withDSA
    15. // The ECDSA signature algorithms
    16. NONEwithECDSA
    17. SHA1withECDSA
    18. SHA256withECDSA
    19. SHA384withECDSA
    20. SHA512withECDSA

    使用

    1. byte[] data = "我是一段测试字符串".getBytes();
    2. Sign sign = SecureUtil.sign(SignAlgorithm.MD5withRSA);
    3. //签名
    4. byte[] signed = sign.sign(data);
    5. //验证签名
    6. boolean verify = sign.verify(data, signed);