Created
October 13, 2016 08:07
-
-
Save djekl/98f6961fd70778fb05dd2cbcb04ae81d to your computer and use it in GitHub Desktop.
Does anybody know how to convert this to PHP correctly, and can provide a derived key in java, for comparison in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static byte[] deriveSP800108HmacSHA256Key(final int n, final byte[] array, final String s, final byte[] array2) { | |
final ByteBuffer allocate = ByteBuffer.allocate(n); | |
final ByteBuffer allocate2 = ByteBuffer.allocate(4); | |
final Mac initializedHmacSha256Digester = Cryptography.getInitializedHmacSha256Digester(new SecretKeySpec(array, "HmacSHA256")); | |
int n2 = 1; | |
while (allocate.position() < n) { | |
initializedHmacSha256Digester.reset(); | |
allocate2.clear(); | |
allocate2.putInt(n2); | |
allocate2.rewind(); | |
initializedHmacSha256Digester.update(allocate2); | |
initializedHmacSha256Digester.update(s.getBytes(Strings.Utf8Charset)); | |
initializedHmacSha256Digester.update((byte)0); | |
initializedHmacSha256Digester.update(array2); | |
allocate2.clear(); | |
allocate2.putInt(n * 8); | |
allocate2.rewind(); | |
initializedHmacSha256Digester.update(allocate2); | |
final byte[] doFinal = initializedHmacSha256Digester.doFinal(); | |
int n3; | |
if ((n3 = doFinal.length) > allocate.remaining()) { | |
n3 = allocate.remaining(); | |
} | |
allocate.put(doFinal, 0, n3); | |
++n2; | |
} | |
return allocate.array(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The spec is here if you understand this sort of thing - http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-108.pdf