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

Use HmacSHA256 provided by JRE

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