HMAC-SHA256 Algorithm for signature calculation

后端 未结 9 598
北海茫月
北海茫月 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();
        }
        ...
    
    0 讨论(0)
  • 2020-12-12 20:19

    The 0x just denotes that the characters after it represent a hex string.

    0x1A == 1Ah == 26 == 1A
    

    So the 0x is just to clarify what format the output is in, no need to worry about it.

    0 讨论(0)
  • 2020-12-12 20:22

    Java simple code to generate encoded(HMAC-x) signatures. (Tried using Java-8 and Eclipse)

    import java.io.UnsupportedEncodingException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    
    import javax.crypto.Mac;
    import javax.crypto.spec.SecretKeySpec;
    
    import com.sun.org.apache.xml.internal.security.utils.Base64;
    
    /**
     * Encryption class to show how to generate encoded(HMAC-x) signatures.
     * 
     */
    public class Encryption {
    
        public static void main(String args[]) {
    
            String message = "This is my message.";
            String key = "your_key";
            String algorithm = "HmacMD5";  // OPTIONS= HmacSHA512, HmacSHA256, HmacSHA1, HmacMD5
    
            try {
    
                // 1. Get an algorithm instance.
                Mac sha256_hmac = Mac.getInstance(algorithm);
    
                // 2. Create secret key.
                SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), algorithm);
    
                // 3. Assign secret key algorithm.
                sha256_hmac.init(secret_key);
    
                // 4. Generate Base64 encoded cipher string.
                String hash = Base64.encode(sha256_hmac.doFinal(message.getBytes("UTF-8")));
    
                // You can use any other encoding format to get hash text in that encoding.
                System.out.println(hash);
    
                /**
                 * Here are the outputs for given algorithms:-
                 * 
                 * HmacMD5 = hpytHW6XebJ/hNyJeX/A2w==
                 * HmacSHA1 = CZbtauhnzKs+UkBmdC1ssoEqdOw=
                 * HmacSHA256 =gCZJBUrp45o+Z5REzMwyJrdbRj8Rvfoy33ULZ1bySXM=
                 * HmacSHA512 = OAqi5yEbt2lkwDuFlO6/4UU6XmU2JEDuZn6+1pY4xLAq/JJGSNfSy1if499coG1K2Nqz/yyAMKPIx9C91uLj+w==
                 */
    
            } catch (NoSuchAlgorithmException e) {
    
                e.printStackTrace();
    
            } catch (UnsupportedEncodingException e) {
    
                e.printStackTrace();
    
            } catch (InvalidKeyException e) {
    
                e.printStackTrace();
    
            }
    
        }
    
    }
    

    NOTE: You can use any other Algorithms and can try generating HmacMD5, HmacSHA1, HmacSHA256, HmacSHA512 signatures.

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