jsSHA, CryptoJS and OpenSSL libraries giving different results

前端 未结 1 941
眼角桃花
眼角桃花 2021-01-03 02:44

New to JS, I\'m also learning to use crypto libraries. I don\'t understand why signing/encoding the same message with the same secret yields differing results.

I\'m

相关标签:
1条回答
  • 2021-01-03 03:25

    There are 3 errors in your code :)

    You're missing the enc-base64-min.js for crypto-js. Without it, CryptoJS.enc.Base64 will be undefined
    You're missing a parameter when calling .getHMAC(). It's .getHMAC(secret, secret_type, hash_type, output_encoding)
    With 1+2 adding a = isn't necessary (nor right)

    <script src="lib/jsSHA/src/sha1.js"></script>  
    <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha1.js"></script>  
    <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js"></script>  
    
    var message = "shah me";
    var secret = "hide me";
    var crypto = CryptoJS.HmacSHA1(message, secret).toString(CryptoJS.enc.Base64);
    var shaObj = new jsSHA(message, "ASCII");
    var jssha = shaObj.getHMAC(secret, "ASCII", "SHA-1", "B64");
    
    return  "crypto answer is " + crypto + " jssha answer is " + jssha;
    

    Example

    0 讨论(0)
提交回复
热议问题