简体   繁体   English

将字符串转换为 SHA1 和 base64

[英]Convert string to SHA1 and to base64

I am having problem with converting the string, lets say: "TestPassword" to SHA1 and to base64.我在转换字符串时遇到问题,可以说:“TestPassword”到 SHA1 和 base64。

Generally according to that site: http://www.online-convert.com/result/1f76972748a7d186198171e9a11e9493一般按照那个网站: 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.将字符串转换为 SHA1 没有问题,但我不知道如何将其再次转换为 base64,因为需要将每两个字符视为一个十六进制字节,然后将其传递给 base64 函数。

Could someone please throw light on it or paste code snippet how to do it?有人可以请说明一下或粘贴代码片段怎么做吗?

Thank You!谢谢!

The Base64 encoding should be performed on the raw binary version of the SHA1 digest, not the hex encoding of it. 应该在SHA1摘要的原始二进制版本上执行Base64编码,而不是它的十六进制编码。

You can get that raw version by passing true for the $raw_output parameter of the sha1() function: 您可以通过为sha1()函数的$raw_output参数传递true来获取该原始版本:

$base64 = base64_encode(sha1("TestPassword", true));

我认为这是一个迟到的回应,但我通过使用原始md5然后将其转换为base64达到了确切的价值:

$base64 = base64_encode(hash("md5", "myPassword", true));

嗨,请在终端中运行此命令,

echo 6250625B226DF62870AE23AF8D3FAC0760D71588 | xxd -r -p | openssl base64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM