· 7 years ago · Oct 13, 2018, 02:48 PM
1$file = 'TESTXML.xml';
2$fp = fopen($file, "r");
3$string = fread($fp,filesize($file));
4$secret = 'somesecret';
5$ctx = hash_init('sha256');
6$sig = hash_hmac('sha256', $string, $secret);
7
8for ($i=0; $i < 2481 ; $i++) {
9 hash_update($ctx, $sig);
10}
11$hash = hash_final($ctx, true);
12echo base64_encode($hash);
13
14private static String generateHash(final InputStream is, final int iteration, final String key) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
15 Mac sha256_HMAC;
16 sha256_HMAC = Mac.getInstance(ALGORITHM);
17 final SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), ALGORITHM);
18 sha256_HMAC.init(secret_key);
19 byte[] bytesBuffer = new byte[2048];
20 int bytesRead = -1;
21
22 while ((bytesRead = is.read(bytesBuffer)) != -1) {
23 sha256_HMAC.update(bytesBuffer, 0, bytesRead);
24 }
25 byte[] digestValue = sha256_HMAC.doFinal();
26 for (int i = 0; i < iteration; i++) {
27 sha256_HMAC.reset();
28 digestValue = sha256_HMAC.doFinal(digestValue);
29 }
30 final String generatedHash = Base64.encodeBase64String(digestValue);
31 return generatedHash;
32 }