How can I create PHP function to output as same as this Java function of hash digest











up vote
0
down vote

favorite
1












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);
}









share|improve this question
























  • 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 $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










  • Well, you need to use a hash_update before the hash_final. It's not exactly like Java.
    – RealSkeptic
    Feb 25 at 11:09















up vote
0
down vote

favorite
1












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);
}









share|improve this question
























  • 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 $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










  • Well, you need to use a hash_update before the hash_final. It's not exactly like Java.
    – RealSkeptic
    Feb 25 at 11:09













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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);
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 $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










  • 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










  • 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










  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."