Class: PasswordManager
- Inherits:
-
Object
- Object
- PasswordManager
- Defined in:
- lib/kafo/password_manager.rb
Instance Method Summary collapse
- #aes_decrypt(text, passphrase) ⇒ Object
- #aes_encrypt(text, passphrase) ⇒ Object
-
#password(n = 32) ⇒ Object
generate a random password of lenght n.
Instance Method Details
#aes_decrypt(text, passphrase) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/kafo/password_manager.rb', line 35 def aes_decrypt(text, passphrase) cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc") cipher.decrypt cipher.key = Digest::SHA2.hexdigest(passphrase) cipher.iv = Digest::SHA2.hexdigest(passphrase + passphrase) decrypted = cipher.update(Base64.decode64(text)) decrypted << cipher.final decrypted end |
#aes_encrypt(text, passphrase) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/kafo/password_manager.rb', line 24 def aes_encrypt(text, passphrase) cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc") cipher.encrypt cipher.key = Digest::SHA2.hexdigest(passphrase) cipher.iv = Digest::SHA2.hexdigest(passphrase + passphrase) encrypted = cipher.update(text) encrypted << cipher.final Base64.encode64(encrypted) end |
#password(n = 32) ⇒ Object
generate a random password of lenght n
on ruby >= 1.9 we use builtin method urlsafe_base64, on olders we use our own implementation (inspired by urlsafe_base64)
the result may contain A-Z, a-z, 0-9, “-” and “_”. “=”
14 15 16 17 18 19 20 21 22 |
# File 'lib/kafo/password_manager.rb', line 14 def password(n = 32) return SecureRandom.urlsafe_base64(n) if SecureRandom.respond_to?(:urlsafe_base64) s = [SecureRandom.random_bytes(n)].pack("m*") s.delete!("\n") s.tr!("+/", "-_") s.delete!("=") s end |