tags:

views:

196

answers:

2

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(password, salt)
A: 

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)
Jordan
+1  A: 

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(/ .)
Michael Kohl