Class: Kyle
- Inherits:
-
Object
- Object
- Kyle
- Defined in:
- lib/kyle.rb
Overview
Password generation from 4 inputs
Instance Attribute Summary collapse
-
#account ⇒ Object
Returns the value of attribute account.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#key ⇒ Object
Returns the value of attribute key.
-
#port ⇒ Object
Returns the value of attribute port.
Class Method Summary collapse
-
.encrypt(alg, val, key) ⇒ Object
Class methods #.
- .hash(alg, val) ⇒ Object
- .hash_to_password(val) ⇒ Object
- .hash_twice(val) ⇒ Object
- .iterative_encrypt(val, key) ⇒ Object
- .iterative_hash(val) ⇒ Object
- .sha512ize(val) ⇒ Object
Instance Method Summary collapse
- #encrypted_inputs ⇒ Object
- #generate ⇒ Object
- #hashed_account ⇒ Object
- #hashed_hostname ⇒ Object
- #hashed_key ⇒ Object
- #hashed_port ⇒ Object
-
#initialize(hostname, account, port, key) ⇒ Kyle
constructor
A new instance of Kyle.
- #passwords ⇒ Object
Constructor Details
#initialize(hostname, account, port, key) ⇒ Kyle
10 11 12 13 14 15 |
# File 'lib/kyle.rb', line 10 def initialize(hostname, account, port, key) @hostname = hostname @account = account @port = port @key = key end |
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
8 9 10 |
# File 'lib/kyle.rb', line 8 def account @account end |
#hostname ⇒ Object
Returns the value of attribute hostname.
8 9 10 |
# File 'lib/kyle.rb', line 8 def hostname @hostname end |
#key ⇒ Object
Returns the value of attribute key.
8 9 10 |
# File 'lib/kyle.rb', line 8 def key @key end |
#port ⇒ Object
Returns the value of attribute port.
8 9 10 |
# File 'lib/kyle.rb', line 8 def port @port end |
Class Method Details
.encrypt(alg, val, key) ⇒ Object
Class methods #
61 62 63 64 65 66 67 68 69 |
# File 'lib/kyle.rb', line 61 def self.encrypt(alg, val, key) cipher = OpenSSL::Cipher::Cipher.new(alg) cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(key, 'kyle', 10_000, 32) cipher.iv = sha512ize(key) cipher.encrypt return cipher.update(val) + cipher.final rescue OpenSSL::Cipher::CipherError => e puts "Error: #{e.message}" end |
.hash(alg, val) ⇒ Object
82 83 84 |
# File 'lib/kyle.rb', line 82 def self.hash(alg, val) OpenSSL::Digest.digest(alg, val) end |
.hash_to_password(val) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/kyle.rb', line 105 def self.hash_to_password(val) ret = '' # to be sure it is long enough hashed = sha512ize(val) hashed.each_byte do |c| ret += Constants::PASSWORD_CHARS[c % Constants::PASSWORD_CHARS.length] break if ret.length >= 16 end ret[0..15] end |
.hash_twice(val) ⇒ Object
101 102 103 |
# File 'lib/kyle.rb', line 101 def self.hash_twice(val) iterative_hash(iterative_hash(val)) end |
.iterative_encrypt(val, key) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/kyle.rb', line 71 def self.iterative_encrypt(val, key) ret = val val.each_byte do |c| alg = Constants::ENC_ALGS[c % Constants::ENC_ALGS.length] ret = encrypt(alg, ret, key) end ret end |
.iterative_hash(val) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/kyle.rb', line 86 def self.iterative_hash(val) ret = val val.each_byte do |c| alg = Constants::HASH_ALGS[c % Constants::HASH_ALGS.length] ret = hash(alg, ret) end ret end |
.sha512ize(val) ⇒ Object
97 98 99 |
# File 'lib/kyle.rb', line 97 def self.sha512ize(val) hash('sha512', val) end |
Instance Method Details
#encrypted_inputs ⇒ Object
52 53 54 55 56 |
# File 'lib/kyle.rb', line 52 def encrypted_inputs Kyle.iterative_encrypt( Kyle.iterative_encrypt(hashed_hostname, hashed_account), Kyle.iterative_encrypt(hashed_port, hashed_key)) end |
#generate ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/kyle.rb', line 21 def generate passwords = {} salt = encrypted_inputs cipher = salt Constants::ANIMALS.each do |a| cipher = OpenSSL::PKCS5.pbkdf2_hmac_sha1(cipher, salt, 10_000, 32) passwords[a] = Kyle.hash_to_password(cipher) end passwords end |
#hashed_account ⇒ Object
40 41 42 |
# File 'lib/kyle.rb', line 40 def hashed_account Kyle.hash_twice(account) end |
#hashed_hostname ⇒ Object
36 37 38 |
# File 'lib/kyle.rb', line 36 def hashed_hostname Kyle.hash_twice(hostname) end |
#hashed_key ⇒ Object
48 49 50 |
# File 'lib/kyle.rb', line 48 def hashed_key Kyle.hash_twice(key) end |
#hashed_port ⇒ Object
44 45 46 |
# File 'lib/kyle.rb', line 44 def hashed_port Kyle.hash_twice(port) end |
#passwords ⇒ Object
17 18 19 |
# File 'lib/kyle.rb', line 17 def passwords @passwords ||= generate end |