How to convert php crypt function (SHA512) to ruby?

為{幸葍}努か 提交于 2019-12-12 09:19:32

问题


note: i am not looking for workaround, i am looking for a plain ruby solution!

this question is the similar to this question, but it isn't answerd, its just a workaround to a shell commando there.

i want to generate a sha512 encrypted string which is compatible with the format in debian /etc/shadow.

the following create a correct string with php:

$salt = 'fGn9LR75';
$hash = crypt('test', '$6$'.$salt);
// hash is:
// $6$fGn9LR75$YpI/vJHjEhvrYp5/eUSRinpiXdMthCxFWSEo0ktFNUaRBsA7pCWYzzmQptmnfyHno9YEJFNHYuESj3nAQmSzc1

as far as i know this a normal, salted base64 encoded string. the spec of the sha generation method is here


回答1:


irb(main):001:0> salt = 'fGn9LR75';
irb(main):002:0* hash = 'test'.crypt('$6$' + salt);
irb(main):003:0* hash
=> "$6$fGn9LR75$YpI/vJHjEhvrYp5/eUSRinpiXdMthCxFWSEo0ktFNUaRBsA7pCWYzzmQptmnfyHno9YEJFNHYuESj3nAQmSzc1"

The crypt() algorithm for SHA256/512 is not simply a base64-encoded hash. It's an intentionally crazy process which involves multiple hashes running in parallel.



来源:https://stackoverflow.com/questions/9043017/how-to-convert-php-crypt-function-sha512-to-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!