Skip to content

Instantly share code, notes, and snippets.

@djekl
Created October 13, 2016 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djekl/98f6961fd70778fb05dd2cbcb04ae81d to your computer and use it in GitHub Desktop.
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
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();
}
@djekl
Copy link
Author

djekl commented Oct 13, 2016

The spec is here if you understand this sort of thing - http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-108.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment