提交 1f4677bb authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Use HmacSHA256 provided by JRE

上级 e5b24c9f
......@@ -5,11 +5,14 @@
*/
package org.h2.security;
import java.security.DigestException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.h2.util.Bits;
/**
......@@ -17,16 +20,7 @@ import org.h2.util.Bits;
*/
public class SHA256 {
private final MessageDigest md;
private final byte[] result = new byte[32];
private SHA256() {
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
/**
......@@ -85,24 +79,13 @@ public class SHA256 {
*/
public static byte[] getHMAC(byte[] key, byte[] message) {
key = normalizeKeyForHMAC(key);
int len = message.length;
SHA256 sha = new SHA256();
byte[] iKey = new byte[64 + len];
byte[] oKey = new byte[64 + 32];
sha.calculateHMAC(key, message, len, iKey, oKey);
return sha.result;
Mac mac = initMac(key);
return calculateHMAC(mac, message, message.length);
}
private void calculateHMAC(byte[] key, byte[] message, int len,
byte[] iKey, byte[] oKey) {
Arrays.fill(iKey, 0, 64, (byte) 0x36);
xor(iKey, key, 64);
System.arraycopy(message, 0, iKey, 64, len);
calculateHash(iKey, 64 + len);
Arrays.fill(oKey, 0, 64, (byte) 0x5c);
xor(oKey, key, 64);
System.arraycopy(result, 0, oKey, 64, 32);
calculateHash(oKey, 64 + 32);
private static byte[] calculateHMAC(Mac mac, byte[] message, int len) {
mac.update(message, 0, len);
return mac.doFinal();
}
private static byte[] normalizeKeyForHMAC(byte[] key) {
......@@ -115,9 +98,13 @@ public class SHA256 {
return key;
}
private static void xor(byte[] target, byte[] data, int len) {
for (int i = 0; i < len; i++) {
target[i] ^= data[i];
private static Mac initMac(byte[] key) {
try {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(key, "HmacSHA256"));
return mac;
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
......@@ -134,11 +121,10 @@ public class SHA256 {
int iterations, int resultLen) {
byte[] result = new byte[resultLen];
byte[] key = normalizeKeyForHMAC(password);
SHA256 sha = new SHA256();
Mac mac = initMac(key);
int len = 64 + Math.max(32, salt.length + 4);
byte[] message = new byte[len];
byte[] iKey = new byte[64 + len];
byte[] oKey = new byte[64 + 32];
byte[] macRes = null;
for (int k = 1, offset = 0; offset < resultLen; k++, offset += 32) {
for (int i = 0; i < iterations; i++) {
if (i == 0) {
......@@ -146,12 +132,12 @@ public class SHA256 {
Bits.writeInt(message, salt.length, k);
len = salt.length + 4;
} else {
System.arraycopy(sha.result, 0, message, 0, 32);
System.arraycopy(macRes, 0, message, 0, 32);
len = 32;
}
sha.calculateHMAC(key, message, len, iKey, oKey);
macRes = calculateHMAC(mac, message, len);
for (int j = 0; j < 32 && j + offset < resultLen; j++) {
result[j + offset] ^= sha.result[j];
result[j + offset] ^= macRes[j];
}
}
}
......@@ -181,13 +167,4 @@ public class SHA256 {
return result;
}
private void calculateHash(byte[] data, int len) {
try {
md.update(data, 0, len);
md.digest(result, 0, 32);
} catch (DigestException e) {
throw new RuntimeException(e);
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论