Title / Description
Code import java.io.*; import java.net.*; import java.security.*; import java.util.*; public class hmacSHA256 { public static String encrypt( String message, String keyStr) { //get the bytes of the keyStr byte[] key = keyStr.getBytes(); // Start by getting an object to generate SHA-256 hashes with. MessageDigest sha256 = null; try { sha256 = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { throw new java.lang.AssertionError(".hmacSHA256(): SHA-256 algorithm not found!"); } // Hash the key if necessary to make it fit in a block (see RFC 2104). if (key.length > 64) { sha256.update(key); key = sha256.digest(); sha256.reset(); } // Pad the key bytes to a block (see RFC 2104). byte block[] = new byte[64]; for (int i = 0; i < key.length; ++i) block[i] = key[i]; for (int i = key.length; i < block.length; ++i) block[i] = 0; // Calculate the inner hash, defined in RFC 2104 as // SHA-256(KEY ^ IPAD + MESSAGE)), where IPAD is 64 bytes of 0x36. for (int i = 0; i < 64; ++i) block[i] ^= 0x36; sha256.update(block); try { sha256.update(message.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new java.lang.AssertionError( "ITunesU.hmacSH256(): UTF-8 encoding not supported!"); } byte[] hash = sha256.digest(); sha256.reset(); // Calculate the outer hash, defined in RFC 2104 as // SHA-256(KEY ^ OPAD + INNER_HASH), where OPAD is 64 bytes of 0x5c. for (int i = 0; i < 64; ++i) block[i] ^= (0x36 ^ 0x5c); sha256.update(block); sha256.update(hash); hash = sha256.digest(); // The outer hash is the message signature... // convert its bytes to hexadecimals. char[] hexadecimals = new char[hash.length * 2]; for (int i = 0; i < hash.length; ++i) { for (int j = 0; j < 2; ++j) { int value = (hash[i] >> (4 - 4 * j)) & 0xf; char base = (value < 10) ? ('0') : ('a' - 10); hexadecimals[i * 2 + j] = (char)(base + value); } } // Return a hexadecimal string representation of the message signature. return new String(hexadecimals); } }
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code