How can I create PHP function to output as same as this Java function of hash digest
up vote
0
down vote
favorite
This is the Java code for the function which create a keyed hash digest, and I want to do the same but in PHP code.
Java code:
private String generateHash(final InputStream is, final int iteration,final String key) throws IOException,NoSuchAlgorithmException,InvalidKeyException {
Mac sha256_HMAC;
sha256_HMAC = Mac.getInstance(ALGORITHM);
final SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), ALGORITHM);
sha256_HMAC.init(secret_key);
byte bytesBuffer = new byte[2048];
int bytesRead = -1;
while ((bytesRead = is.read(bytesBuffer)) != -1) {
sha256_HMAC.update(bytesBuffer, 0, bytesRead);
}
byte digestValue = sha256_HMAC.doFinal();
for (int i = 0; i < iteration; i++) {
sha256_HMAC.reset();
digestValue = sha256_HMAC.doFinal(digestValue);
}
return Base64.encodeBase64String(digestValue);
}
This the PHP code I have tried, but the output of both code are not same.
And also that I think I'm missing something somewhere because I don't know alternative for each code statement of Java in PHP:
function generateHash($file, $iteration, $key){
//$hash = hash_hmac('sha256', 'hello, world!', 'mykey');
$inithash=hash_init('SHA256',1,$key);
//mb_strlen($string, '8bit')
while ($buffer=fread($file,"2048")) {
hash_update($inithash, $buffer);
}
$hash = hash_final($inithash);
$hashbyte = unpack('C*',$hash);
for($i=0;$i<$iteration;$i++)
{
//unset($hashbyte);
$inithash=hash_init('SHA256');
$inithash = hash_final($inithash);
}
return base64_encode($inithash);
}
java php
|
show 1 more comment
up vote
0
down vote
favorite
This is the Java code for the function which create a keyed hash digest, and I want to do the same but in PHP code.
Java code:
private String generateHash(final InputStream is, final int iteration,final String key) throws IOException,NoSuchAlgorithmException,InvalidKeyException {
Mac sha256_HMAC;
sha256_HMAC = Mac.getInstance(ALGORITHM);
final SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), ALGORITHM);
sha256_HMAC.init(secret_key);
byte bytesBuffer = new byte[2048];
int bytesRead = -1;
while ((bytesRead = is.read(bytesBuffer)) != -1) {
sha256_HMAC.update(bytesBuffer, 0, bytesRead);
}
byte digestValue = sha256_HMAC.doFinal();
for (int i = 0; i < iteration; i++) {
sha256_HMAC.reset();
digestValue = sha256_HMAC.doFinal(digestValue);
}
return Base64.encodeBase64String(digestValue);
}
This the PHP code I have tried, but the output of both code are not same.
And also that I think I'm missing something somewhere because I don't know alternative for each code statement of Java in PHP:
function generateHash($file, $iteration, $key){
//$hash = hash_hmac('sha256', 'hello, world!', 'mykey');
$inithash=hash_init('SHA256',1,$key);
//mb_strlen($string, '8bit')
while ($buffer=fread($file,"2048")) {
hash_update($inithash, $buffer);
}
$hash = hash_final($inithash);
$hashbyte = unpack('C*',$hash);
for($i=0;$i<$iteration;$i++)
{
//unset($hashbyte);
$inithash=hash_init('SHA256');
$inithash = hash_final($inithash);
}
return base64_encode($inithash);
}
java php
What have you tried so far? What is not working?
– BenRoob
Feb 25 at 10:22
i have updated the php code which i tried , please have look at it once.
– shashikant kuswaha
Feb 25 at 10:32
You noticed that you're not actually using the$hashor the$hashbytevalues in your loop, haven't you?
– RealSkeptic
Feb 25 at 10:57
yes , i know but i dont know that how to use that because they hash_final() accepts only one param. which is hash_init() output. and there is where i m confused .
– shashikant kuswaha
Feb 25 at 10:59
Well, you need to use a hash_update before the hash_final. It's not exactly like Java.
– RealSkeptic
Feb 25 at 11:09
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This is the Java code for the function which create a keyed hash digest, and I want to do the same but in PHP code.
Java code:
private String generateHash(final InputStream is, final int iteration,final String key) throws IOException,NoSuchAlgorithmException,InvalidKeyException {
Mac sha256_HMAC;
sha256_HMAC = Mac.getInstance(ALGORITHM);
final SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), ALGORITHM);
sha256_HMAC.init(secret_key);
byte bytesBuffer = new byte[2048];
int bytesRead = -1;
while ((bytesRead = is.read(bytesBuffer)) != -1) {
sha256_HMAC.update(bytesBuffer, 0, bytesRead);
}
byte digestValue = sha256_HMAC.doFinal();
for (int i = 0; i < iteration; i++) {
sha256_HMAC.reset();
digestValue = sha256_HMAC.doFinal(digestValue);
}
return Base64.encodeBase64String(digestValue);
}
This the PHP code I have tried, but the output of both code are not same.
And also that I think I'm missing something somewhere because I don't know alternative for each code statement of Java in PHP:
function generateHash($file, $iteration, $key){
//$hash = hash_hmac('sha256', 'hello, world!', 'mykey');
$inithash=hash_init('SHA256',1,$key);
//mb_strlen($string, '8bit')
while ($buffer=fread($file,"2048")) {
hash_update($inithash, $buffer);
}
$hash = hash_final($inithash);
$hashbyte = unpack('C*',$hash);
for($i=0;$i<$iteration;$i++)
{
//unset($hashbyte);
$inithash=hash_init('SHA256');
$inithash = hash_final($inithash);
}
return base64_encode($inithash);
}
java php
This is the Java code for the function which create a keyed hash digest, and I want to do the same but in PHP code.
Java code:
private String generateHash(final InputStream is, final int iteration,final String key) throws IOException,NoSuchAlgorithmException,InvalidKeyException {
Mac sha256_HMAC;
sha256_HMAC = Mac.getInstance(ALGORITHM);
final SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), ALGORITHM);
sha256_HMAC.init(secret_key);
byte bytesBuffer = new byte[2048];
int bytesRead = -1;
while ((bytesRead = is.read(bytesBuffer)) != -1) {
sha256_HMAC.update(bytesBuffer, 0, bytesRead);
}
byte digestValue = sha256_HMAC.doFinal();
for (int i = 0; i < iteration; i++) {
sha256_HMAC.reset();
digestValue = sha256_HMAC.doFinal(digestValue);
}
return Base64.encodeBase64String(digestValue);
}
This the PHP code I have tried, but the output of both code are not same.
And also that I think I'm missing something somewhere because I don't know alternative for each code statement of Java in PHP:
function generateHash($file, $iteration, $key){
//$hash = hash_hmac('sha256', 'hello, world!', 'mykey');
$inithash=hash_init('SHA256',1,$key);
//mb_strlen($string, '8bit')
while ($buffer=fread($file,"2048")) {
hash_update($inithash, $buffer);
}
$hash = hash_final($inithash);
$hashbyte = unpack('C*',$hash);
for($i=0;$i<$iteration;$i++)
{
//unset($hashbyte);
$inithash=hash_init('SHA256');
$inithash = hash_final($inithash);
}
return base64_encode($inithash);
}
java php
java php
edited Nov 10 at 13:15
Zoe
10.2k73475
10.2k73475
asked Feb 25 at 10:19
shashikant kuswaha
1227
1227
What have you tried so far? What is not working?
– BenRoob
Feb 25 at 10:22
i have updated the php code which i tried , please have look at it once.
– shashikant kuswaha
Feb 25 at 10:32
You noticed that you're not actually using the$hashor the$hashbytevalues in your loop, haven't you?
– RealSkeptic
Feb 25 at 10:57
yes , i know but i dont know that how to use that because they hash_final() accepts only one param. which is hash_init() output. and there is where i m confused .
– shashikant kuswaha
Feb 25 at 10:59
Well, you need to use a hash_update before the hash_final. It's not exactly like Java.
– RealSkeptic
Feb 25 at 11:09
|
show 1 more comment
What have you tried so far? What is not working?
– BenRoob
Feb 25 at 10:22
i have updated the php code which i tried , please have look at it once.
– shashikant kuswaha
Feb 25 at 10:32
You noticed that you're not actually using the$hashor the$hashbytevalues in your loop, haven't you?
– RealSkeptic
Feb 25 at 10:57
yes , i know but i dont know that how to use that because they hash_final() accepts only one param. which is hash_init() output. and there is where i m confused .
– shashikant kuswaha
Feb 25 at 10:59
Well, you need to use a hash_update before the hash_final. It's not exactly like Java.
– RealSkeptic
Feb 25 at 11:09
What have you tried so far? What is not working?
– BenRoob
Feb 25 at 10:22
What have you tried so far? What is not working?
– BenRoob
Feb 25 at 10:22
i have updated the php code which i tried , please have look at it once.
– shashikant kuswaha
Feb 25 at 10:32
i have updated the php code which i tried , please have look at it once.
– shashikant kuswaha
Feb 25 at 10:32
You noticed that you're not actually using the
$hash or the $hashbyte values in your loop, haven't you?– RealSkeptic
Feb 25 at 10:57
You noticed that you're not actually using the
$hash or the $hashbyte values in your loop, haven't you?– RealSkeptic
Feb 25 at 10:57
yes , i know but i dont know that how to use that because they hash_final() accepts only one param. which is hash_init() output. and there is where i m confused .
– shashikant kuswaha
Feb 25 at 10:59
yes , i know but i dont know that how to use that because they hash_final() accepts only one param. which is hash_init() output. and there is where i m confused .
– shashikant kuswaha
Feb 25 at 10:59
Well, you need to use a hash_update before the hash_final. It's not exactly like Java.
– RealSkeptic
Feb 25 at 11:09
Well, you need to use a hash_update before the hash_final. It's not exactly like Java.
– RealSkeptic
Feb 25 at 11:09
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48972404%2fhow-can-i-create-php-function-to-output-as-same-as-this-java-function-of-hash-di%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
What have you tried so far? What is not working?
– BenRoob
Feb 25 at 10:22
i have updated the php code which i tried , please have look at it once.
– shashikant kuswaha
Feb 25 at 10:32
You noticed that you're not actually using the
$hashor the$hashbytevalues in your loop, haven't you?– RealSkeptic
Feb 25 at 10:57
yes , i know but i dont know that how to use that because they hash_final() accepts only one param. which is hash_init() output. and there is where i m confused .
– shashikant kuswaha
Feb 25 at 10:59
Well, you need to use a hash_update before the hash_final. It's not exactly like Java.
– RealSkeptic
Feb 25 at 11:09