Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to SHA1 and to base64

Tags:

php

I am having problem with converting the string, lets say: "TestPassword" to SHA1 and to base64.

Generally according to that site: http://www.online-convert.com/result/1f76972748a7d186198171e9a11e9493

I should be given the results below for the above password:

hex: 6250625b226df62870ae23af8d3fac0760d71588
HEX: 6250625B226DF62870AE23AF8D3FAC0760D71588
h:e:x: 62:50:62:5b:22:6d:f6:28:70:ae:23:af:8d:3f:ac:07:60:d7:15:88
base64: YlBiWyJt9ihwriOvjT+sB2DXFYg=  <-- That is what I would like to achieve ...

There is no problem with converting string to SHA1 but i don't know how to convert it again to base64 as there is a need to treat every two characters as a hex byte and then pass it to base64 function.

Could someone please throw light on it or paste code snippet how to do it?

Thank You!

like image 375
John Avatar asked Jan 08 '15 10:01

John


2 Answers

The Base64 encoding should be performed on the raw binary version of the SHA1 digest, not the hex encoding of it.

You can get that raw version by passing true for the $raw_output parameter of the sha1() function:

$base64 = base64_encode(sha1("TestPassword", true));
like image 129
Alnitak Avatar answered Oct 30 '22 13:10

Alnitak


I think is a late response but I achieved that exact value by using the raw md5 and then converting it to base64:

$base64 = base64_encode(hash("md5", "myPassword", true));
like image 22
Giomar Martinez Avatar answered Oct 30 '22 14:10

Giomar Martinez