Class: Kyle

Inherits:
Object
  • Object
show all
Defined in:
lib/kyle.rb

Overview

Password generation from 4 inputs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, account, port, key) ⇒ Kyle



10
11
12
13
14
15
# File 'lib/kyle.rb', line 10

def initialize(hostname, , port, key)
  @hostname = hostname
  @account = 
  @port = port
  @key = key
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



8
9
10
# File 'lib/kyle.rb', line 8

def 
  @account
end

#hostnameObject

Returns the value of attribute hostname.



8
9
10
# File 'lib/kyle.rb', line 8

def hostname
  @hostname
end

#keyObject

Returns the value of attribute key.



8
9
10
# File 'lib/kyle.rb', line 8

def key
  @key
end

#portObject

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_inputsObject



52
53
54
55
56
# File 'lib/kyle.rb', line 52

def encrypted_inputs
  Kyle.iterative_encrypt(
    Kyle.iterative_encrypt(hashed_hostname, ),
    Kyle.iterative_encrypt(hashed_port, hashed_key))
end

#generateObject



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_accountObject



40
41
42
# File 'lib/kyle.rb', line 40

def 
  Kyle.hash_twice()
end

#hashed_hostnameObject



36
37
38
# File 'lib/kyle.rb', line 36

def hashed_hostname
  Kyle.hash_twice(hostname)
end

#hashed_keyObject



48
49
50
# File 'lib/kyle.rb', line 48

def hashed_key
  Kyle.hash_twice(key)
end

#hashed_portObject



44
45
46
# File 'lib/kyle.rb', line 44

def hashed_port
  Kyle.hash_twice(port)
end

#passwordsObject



17
18
19
# File 'lib/kyle.rb', line 17

def passwords
  @passwords ||= generate
end