[英]Why mysql and perl base64 string encoding is different?
#!/usr/bin/env perl
use Digest;
say Digest->new( 'SHA-1' )->add('test')->b64digest; # qUqP5cyxm6YcTAhz05Hph5gvu9M
SELECT TO_BASE64(SHA1('test')); # YTk0YThmZTVjY2IxOWJhNjFjNGMwODczZDM5MWU5ODc5ODJmYmJkMw==
So same ASCII word test
encoded as qUqP5cyxm6YcTAhz05Hph5gvu9M
in Perl and as YTk0YThmZTVjY2IxOWJhNjFjNGMwODczZDM5MWU5ODc5ODJmYmJkMw==
in MySQL.因此,在 Perl 和
YTk0YThmZTVjY2IxOWJhNjFjNGMwODczZDM5MWU5ODc5ODJmYmJkMw==
中编码为qUqP5cyxm6YcTAhz05Hph5gvu9M
的相同 ASCII 字test
。
Why?为什么?
As pointed out by Raymond, you're looking at the results of two different calculations.正如雷蒙德所指出的,您正在查看两种不同计算的结果。
Your perl prints out the base64 encoded SHA-1 digest of "test"
.您的 perl 打印出 base64 编码的 SHA-1 摘要
"test"
。 Your MySQL query takes the base16 encoded SHA-1 digest of "test"
and then base64 encodes that string.您的 MySQL 查询采用 base16 编码的
"test"
SHA-1 摘要,然后 base64 对该字符串进行编码。
Consider a perl one-liner that does the same two steps as the SELECT
:考虑一个 perl 单线,它执行与
SELECT
相同的两个步骤:
$ perl -MDigest -MMIME::Base64 -E 'say encode_base64(Digest->new("SHA-1")->add("test")->hexdigest)'
YTk0YThmZTVjY2IxOWJhNjFjNGMwODczZDM5MWU5ODc5ODJmYmJkMw==
and the MySQL query和MySQL 查询
SELECT to_base64(unhex(sha1('test')))
which gives qUqP5cyxm6YcTAhz05Hph5gvu9M=
, like your perl but with padding added.这给出了
qUqP5cyxm6YcTAhz05Hph5gvu9M=
,就像你的 perl 但添加了填充。
I'd stick with base16/hex versions, though, as that's what people are used to seeing for digests: a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
for the SHA-1 of "test"
.不过,我会坚持使用 base16/hex 版本,因为这是人们习惯于在摘要中看到的内容:
a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
用于"test"
的 SHA-1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.