HMAC-SHA256 Algorithm for signature calculation

后端 未结 9 736
北海茫月
北海茫月 2020-12-12 19:27

I am trying to create a signature using the HMAC-SHA256 algorithm and this is my code. I am using US ASCII encoding.

final Charset asciiCs = Charset.forName(         


        
9条回答
  •  佛祖请我去吃肉
    2020-12-12 20:18

    The answer that you got there is correct. One minor thing in the code above, you need to init(key) before you can call doFinal()

        final Charset charSet = Charset.forName("US-ASCII");
        final Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
    
        final SecretKeySpec secret_key = new javax.crypto.spec.SecretKeySpec(charSet.encode("key").array(), "HmacSHA256");
        try {
            sha256_HMAC.init(secret_key);
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ...
    

提交回复
热议问题