100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > C#实现php的hash_hmac函数

C#实现php的hash_hmac函数

时间:2022-09-27 05:28:14

相关推荐

C#实现php的hash_hmac函数

from:/ciaos/article/details/12618487

PHP代码示例如下

<?php

$res1=hash_hmac("sha1","signatureString","secret");

echo$res1."\n";

//ee1b654aa861c41fd5813dc365ef106c9801f8f6

echobase64_encode($res1)."\n";

//ZWUxYjY1NGFhODYxYzQxZmQ1ODEzZGMzNjVlZjEwNmM5ODAxZjhmNg==

$res2=hash_hmac("sha1","signatureString","secret",true);

//echo"$res2\n";//binarybyte[]

$data=unpack('C*',$res2);

print_r($data);

/*

Array

(

[1]=>238

[2]=>27

[3]=>101

[4]=>74

[5]=>168

[6]=>97

[7]=>196

[8]=>31

[9]=>213

[10]=>129

[11]=>61

[12]=>195

[13]=>101

[14]=>239

[15]=>16

[16]=>108

[17]=>152

[18]=>1

[19]=>248

[20]=>246

)

*/

echobase64_encode("$res2\n");

//7htlSqhhxB/VgT3DZe8QbJgB+PYK

?>

C#代码示例如下

usingSystem;

usingSystem.IO;

usingSystem.Collections;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Security.Cryptography;

namespaceConsoleApplication1

{

classProgram

{

privateObjecthash_hmac(stringsignatureString,stringsecretKey,boolraw_output=false)

{

varenc=Encoding.UTF8;

HMACSHA1hmac=newHMACSHA1(enc.GetBytes(secretKey));

hmac.Initialize();

byte[]buffer=enc.GetBytes(signatureString);

if(raw_output)

{

puteHash(buffer);

}

else

{

returnBitConverter.ToString(puteHash(buffer)).Replace("-","").ToLower();

}

}

staticvoidMain(string[]args)

{

Programp=newProgram();

Stringres1=(String)p.hash_hmac("signatureString","secret");

Console.WriteLine(res1);

//ee1b654aa861c41fd5813dc365ef106c9801f8f6

Console.WriteLine(Convert.ToBase64String(Encoding.UTF8.GetBytes(res1)));

//ZWUxYjY1NGFhODYxYzQxZmQ1ODEzZGMzNjVlZjEwNmM5ODAxZjhmNg==

byte[]ret=(byte[])p.hash_hmac("signatureString","secret",true);

for(inti=0;i<ret.Length;i++){

Console.Write(ret[i]+"");

}

//2382710174168971963121312961195101239161081521248246

Console.WriteLine();

Console.WriteLine(Convert.ToBase64String(ret));

//7htlSqhhxB/VgT3DZe8QbJgB+PY=

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。