Java Utililty Methods HMAC

List of utility methods to do HMAC

  1. HOME
  2. Java
  3. H
  4. HMAC

Description

The list of methods to do HMAC are organized into topic(s).

Method

byte[] hmac(byte key[], byte text[])
Internal used.
return hmac(key, text, 64);
String hmac(String hash, String data, String key)
hmac
byte[] byteHMAC = null;
try {
 Mac mac = Mac.getInstance("Hmac" + hash);
 SecretKeySpec spec = new SecretKeySpec(key.getBytes(), "Hmac" + hash);
 mac.init(spec);
 byteHMAC = mac.doFinal(data.getBytes("UTF-8"));
} catch (InvalidKeyException e) {
 e.printStackTrace();
...
String hmac(String msg, String keyString)
hmac
String digest = null;
try {
 SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), "HmacMD5");
 Mac mac = Mac.getInstance("HmacMD5");
 mac.init(key);
 byte[] bytes = mac.doFinal(msg.getBytes("UTF-8"));
 StringBuffer hash = new StringBuffer();
 for (int i = 0; i < bytes.length; i++) {
...
String HMAC(String username, BigInteger nonce, String hashedPassword)
This will Find HMAC and Append it to
String hash = null;
StringBuilder sb = new StringBuilder(14);
sb.append(username).append(" ").append(nonce).append(" ").append(hashedPassword);
String appended = sb.toString();
try {
 hash = makeSHA512Hash(appended);
} catch (UnsupportedEncodingException e) {
 e.printStackTrace();
...
byte[] hmac_sha(String crypto, byte[] keyBytes, byte[] text)
This method uses the JCE to provide the crypto algorithm.
try {
 Mac hmac;
 hmac = Mac.getInstance(crypto);
 SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW");
 hmac.init(macKey);
 return hmac.doFinal(text);
} catch (GeneralSecurityException gse) {
 throw new UndeclaredThrowableException(gse);
...
byte[] hmac_sha(String crypto, byte[] keyBytes, byte[] text)
This method uses the JCE to provide the crypto algorithm.
try {
 Mac hmac;
 hmac = Mac.getInstance(crypto);
 SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW");
 hmac.init(macKey);
 return hmac.doFinal(text);
} catch (GeneralSecurityException gse) {
 throw new UndeclaredThrowableException(gse);
...
String hmacDigest(String message, String secretKey, String algorithm)
hmac Digest
String digest = null;
try {
 SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), algorithm);
 Mac mac = Mac.getInstance(algorithm);
 mac.init(key);
 byte[] bytes = mac.doFinal(message.getBytes(StandardCharsets.US_ASCII));
 digest = toHex(bytes);
} catch (InvalidKeyException e) {
...
String hmacEncode(String algorithm, String input, String privateKey)
hmac Encode
try {
 byte[] keyBytes = privateKey.getBytes();
 Key key = new SecretKeySpec(keyBytes, 0, keyBytes.length, algorithm);
 Mac mac = Mac.getInstance(algorithm);
 mac.init(key);
 return byteArrayToHex(mac.doFinal(input.getBytes()));
} catch (NoSuchAlgorithmException ex) {
 throw new IllegalArgumentException("Unknown algorithm: " + algorithm);
...
String hmacHex(String keyString, String message, String hmac)
hmac Hex
try {
 SecretKey key = new SecretKeySpec(keyString.getBytes("UTF-8"), hmac);
 Mac mac = Mac.getInstance(key.getAlgorithm());
 mac.init(key);
 return encodeHex(mac.doFinal(message.getBytes("UTF-8")));
} catch (UnsupportedEncodingException | NoSuchAlgorithmException | InvalidKeyException e) {
 return "";
byte[] hmacSha(byte[] keyBytes, byte[] text)
hmac Sha
try {
 Mac hmac;
 hmac = Mac.getInstance("HmacSHA1");
 SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW");
 hmac.init(macKey);
 return hmac.doFinal(text);
} catch (GeneralSecurityException gse) {
 throw new UndeclaredThrowableException(gse);
...


AltStyle によって変換されたページ (->オリジナル) /