Page 1 of 1

Help - C# Equilant to base64_encode(pack("H*",$sha1Si)

Posted: Fri Jan 27, 2012 8:20 pm
by Isystems
Hi,

Can anyone please give equilant to the following php code in C#?

$sha1Signature = sha1($toEncrypt)
//This will give b14dcc7842a53f1ec7a621e77c106dfbe8283779
//Encode the signature using Base64 before transmitting.

$base64Sha1Signature = base64_encode(pack("H*",$sha1Signature));
//This will give sU3MeEKlPx7HpiHnfBBt++goN3k=

My code is calculting SHA1 fine but Base 64 Encode Result is different. When I send "b14dcc7842a53f1ec7a621e77c106dfbe8283779" to the following function it gives "ZmVkMzg5ZjJlNjM0ZmE2YjYyYmRmYmZhZmQwNWJlNzYxMTc2Y2VlOQ==" but I want it to be "sU3MeEKlPx7HpiHnfBBt++goN3k=".

static public string EncodeTo64(string toEncode)
{
byte[] encbuff = System.Text.Encoding.ASCII.GetBytes(toEncode);
return Convert.ToBase64String(encbuff);
}

Thanks.

Kind Regards,

Sol

Re: Help - C# Equilant to base64_encode(pack("H*",$sha1Si)

Posted: Sat Jan 28, 2012 10:20 am
by Isystems
We need to send Byet[] to Encode Function

public string crypt(string data)
{
SHA1 sha = new SHA1Managed();
return Convert.ToBase64String(sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(data)));
}