HMAC-SHA256 Algorithm for signature calculation

后端 未结 9 738
北海茫月
北海茫月 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条回答
  •  萌比男神i
    2020-12-12 19:56

    Here is my solution:

    public String HMAC_SHA256(String secret, String message)
    {
        String hash="";
        try{
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secret_key);
    
            hash = Base64.encodeToString(sha256_HMAC.doFinal(message.getBytes()), Base64.DEFAULT);
        }catch (Exception e)
        {
    
        }
        return hash.trim();
    }
    

提交回复
热议问题