开发者

python crypt.crypt in ruby?

hi i need this code in ruby I don't know how I write the crypt.crypt method in ruby, any ideas?

(I want to simulate the linux comand .htpasswd)

import random
import crypt

letters = 'abcdefghijklmnopqrstuvwxyz' \
          'ABCDEFGHIJKLMNOPQRSTUVWXYZ' \
          '0123456789/.'
salt = random.choice(letters) + random.choice(letters)

password = "bla"

print crypt.crypt(pass开发者_如何学Cword, salt)


Jordan already told you about String#crypt, so I'll just show you an easier way to create your letters array:

letters = [*'a'..'z'] + [*'A'..'Z'] + [*0..9] + %w(/ .)

Update: since this got upvoted after more than 2 years, I might as well add the 1.9 way of doing this (using multiple splats and character literals):

letters = [*?a..?z, *?A..?Z, *0..9, ?/, ?.]


I believe Ruby's String#crypt is equivalent to Python's crypt.crypt, so the Ruby equivalent to your code would be something like:

letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/.'
salt = letters[rand letters.length].chr + letters[rand letters.length].chr

password = "bla"

puts password.crypt(salt)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜