提交 1631f879 authored 作者: Thomas Mueller's avatar Thomas Mueller

Javadocs

上级 ac65d7c1
...@@ -84,6 +84,13 @@ public class SHA256 { ...@@ -84,6 +84,13 @@ public class SHA256 {
return getHash(buff, true); return getHash(buff, true);
} }
/**
* Calculate the hash-based message authentication code.
*
* @param key the key
* @param message the message
* @return the hash
*/
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; int len = message.length;
...@@ -98,7 +105,8 @@ public class SHA256 { ...@@ -98,7 +105,8 @@ public class SHA256 {
return sha.result; return sha.result;
} }
private void calculateHMAC(byte[] key, byte[] message, int len, byte[] iKey, byte[] oKey, byte[] byteBuff, int[] intBuff) { private void calculateHMAC(byte[] key, byte[] message, int len,
byte[] iKey, byte[] oKey, byte[] byteBuff, int[] intBuff) {
Arrays.fill(iKey, 0, 64, (byte) 0x36); Arrays.fill(iKey, 0, 64, (byte) 0x36);
xor(iKey, key, 64); xor(iKey, key, 64);
System.arraycopy(message, 0, iKey, 64, len); System.arraycopy(message, 0, iKey, 64, len);
...@@ -125,6 +133,15 @@ public class SHA256 { ...@@ -125,6 +133,15 @@ public class SHA256 {
} }
} }
/**
* Calculate the hash using the password-based key derivation function 2.
*
* @param password the password
* @param salt the salt
* @param iterations the number of iterations
* @param resultLen the number of bytes in the result
* @return the result
*/
public static byte[] getPBKDF2(byte[] password, byte[] salt, int iterations, int resultLen) { public static byte[] getPBKDF2(byte[] password, byte[] salt, int iterations, int resultLen) {
byte[] result = new byte[resultLen]; byte[] result = new byte[resultLen];
byte[] key = normalizeKeyForHMAC(password); byte[] key = normalizeKeyForHMAC(password);
......
...@@ -82,8 +82,8 @@ public class TestSecurity extends TestBase { ...@@ -82,8 +82,8 @@ public class TestSecurity extends TestBase {
"348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9", "348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9",
StringUtils.convertBytesToHex( StringUtils.convertBytesToHex(
SHA256.getPBKDF2( SHA256.getPBKDF2(
"passwordPASSWORDpassword".getBytes(), ("password" + "PASSWORD" + "password").getBytes(),
"saltSALTsaltSALTsaltSALTsaltSALTsalt".getBytes(), 4096, 40))); ("salt"+ "SALT"+ "salt"+ "SALT"+ "salt"+ "SALT"+ "salt"+ "SALT"+ "salt").getBytes(), 4096, 40)));
assertEquals( assertEquals(
"89b69d0516f829893c696226650a8687", "89b69d0516f829893c696226650a8687",
StringUtils.convertBytesToHex( StringUtils.convertBytesToHex(
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论