1
1
# Cryptography
2
2
3
+ ## Generating Hashes
3
4
4
- ## Generating Hashes
5
+ ### MD5 hash
5
6
6
- ### MD5 hash
7
7
``` ruby
8
8
require ' digest'
9
9
puts Digest ::MD5 .hexdigest ' P@ssw0rd'
10
10
```
11
+
11
12
### SHA1 hash
12
13
13
14
``` ruby
@@ -16,9 +17,10 @@ puts Digest::SHA1.hexdigest 'P@ssw0rd'
16
17
```
17
18
18
19
### SHA2 hash
19
- In SHA2 you have 2 ways to do it
20
20
21
- ** Way #1 :** By creating a new SHA2 hash object with a given bit length.
21
+ In SHA2 you have 2 ways to do it.
22
+
23
+ ** Way \# 1:** By creating a new SHA2 hash object with a given bit length.
22
24
23
25
``` ruby
24
26
require ' digest'
@@ -31,15 +33,26 @@ sha2_256.hexdigest 'P@ssw0rd'
31
33
Digest ::SHA2 .new (bitlen = 256 ).hexdigest ' P@ssw0rd'
32
34
```
33
35
34
- ** Way #2 :** By Using the class directly
36
+ ** Way \# 2:** By Using the class directly
37
+
35
38
``` ruby
36
39
require ' digest'
37
40
puts Digest ::SHA256 .hexdigest ' P@ssw0rd'
38
41
puts Digest ::SHA384 .hexdigest ' P@ssw0rd'
39
42
puts Digest ::SHA512 .hexdigest ' P@ssw0rd'
40
43
```
41
44
45
+ ** Bonus: Generate Linux-like Shadow password**
46
+
47
+ ``` ruby
48
+ require ' digest/sha2'
49
+ password = ' P@ssw0rd'
50
+ salt = rand (36 ** 8 ).to_s(36 )
51
+ shadow_hash = password.crypt(" $6$" + salt)
52
+ ```
53
+
42
54
### Windows LM Password hash
55
+
43
56
``` ruby
44
57
require ' openssl'
45
58
49
62
50
63
def gen_keys (str )
51
64
split7(str).map do |str7 |
52
-
65
+
53
66
bits = split7(str7.unpack(" B*" )[0 ]).inject(' ' ) do |ret , tkn |
54
67
ret += tkn + (tkn.gsub (' 1' , ' ' ).size % 2 ).to_s
55
68
end
56
-
69
+
57
70
[bits].pack(" B*" )
58
71
end
59
72
end
74
87
75
88
puts lm_hash " P@ssw0rd"
76
89
```
77
- [ Source | RubyNTLM] [ 1 ]
90
+
91
+ [ Source \| RubyNTLM] ( https://github.com/wimm/rubyntlm/blob/master/lib/net/ntlm.rb )
78
92
79
93
### Windows NTLMv1 Password hash
94
+
80
95
``` ruby
81
96
require ' openssl'
82
97
ntlmv1 = OpenSSL ::Digest ::MD4 .hexdigest " P@ssw0rd" .encode(' UTF-16LE' )
83
98
puts ntlmv1
84
99
```
85
100
86
101
### Windows NTLMv2 Password hash
102
+
87
103
``` ruby
88
104
require ' openssl'
89
105
ntlmv1 = OpenSSL ::Digest ::MD4 .hexdigest " P@ssw0rd" .encode(' UTF-16LE' )
@@ -93,37 +109,41 @@ puts ntlmv2
93
109
```
94
110
95
111
### MySQL Password hash
112
+
96
113
``` ruby
97
114
puts " *" + Digest ::SHA1 .hexdigest(Digest ::SHA1 .digest(' P@ssw0rd' )).upcase
98
115
```
99
116
100
117
### PostgreSQL Password hash
118
+
101
119
PostgreSQL hashes combined password and username then adds ** md5** in front of the hash
120
+
102
121
``` ruby
103
122
require ' digest/md5'
104
123
puts ' md5' + Digest ::MD5 .hexdigest(' P@ssw0rd' + ' admin' )
105
124
```
106
125
107
- ## Symmetric Encryptions
126
+ ## Symmetric Encryptions
108
127
109
128
To list all supported algorithms
129
+
110
130
``` ruby
111
131
require ' openssl'
112
132
puts OpenSSL ::Cipher .ciphers
113
133
```
114
134
115
- To unserdatand the cipher naming (eg. ` AES-128-CBC ` ), it devided to 3 parts seperated by hyphen ` <Name>-<Key_length>-<Mode> `
135
+ To unserdatand the cipher naming \ ( eg. ` AES-128-CBC ` \ ) , it devided to 3 parts seperated by hyphen ` <Name>-<Key_length>-<Mode> `
116
136
117
137
Symmetric encrption algorithms modes need 3 import data in order to work
118
138
119
- 1 . Key (password)
120
- 2 . Initial Vector (iv)
121
- 3 . Data to encrypt (plain text)
139
+ 1 . Key \ ( password\ )
140
+ 2 . Initial Vector \ ( iv\ )
141
+ 3 . Data to encrypt \ ( plain text\ )
122
142
123
-
124
- ### AES encryption
143
+ ### AES encryption
125
144
126
145
#### Encrypt
146
+
127
147
``` ruby
128
148
require " openssl"
129
149
@@ -135,28 +155,28 @@ cipher.encrypt # Initializes the Cipher for enc
135
155
key = cipher.random_key # If hard coded key, it must be 265-bits length
136
156
iv = cipher.random_iv # Generate iv
137
157
encrypted = cipher.update(data) + cipher.final # Finalize the encryption
138
-
139
158
```
140
159
141
160
#### Dencrypt
161
+
142
162
``` ruby
143
163
decipher = OpenSSL ::Cipher ::AES .new (' 256-CBC' ) # Or use: OpenSSL::Cipher::Cipher.new('AES-256-CBC')
144
164
decipher.decrypt # Initializes the Cipher for dencryption. (Must be called before key, iv, random_key, random_iv)
145
165
decipher.key = key # Or generate secure random key: cipher.random_key
146
166
decipher.iv = iv # Generate iv
147
167
plain = decipher.update(encrypted) + decipher.final # Finalize the dencryption
148
-
149
168
```
150
169
151
170
** Resources**
152
- - [ OpenSSL::Cipher docs] ( https://ruby-doc.org/stdlib-2.3.3/libdoc/openssl/rdoc/OpenSSL/Cipher.html )
153
- - [ (Symmetric) Encryption With Ruby (and Rails)] ( http://stuff-things.net/2015/02/12/symmetric-encryption-with-ruby-and-rails/ )
171
+
172
+ * [ OpenSSL::Cipher docs] ( https://ruby-doc.org/stdlib-2.3.3/libdoc/openssl/rdoc/OpenSSL/Cipher.html )
173
+ * [ \( Symmetric\) Encryption With Ruby \( and Rails\) ] ( http://stuff-things.net/2015/02/12/symmetric-encryption-with-ruby-and-rails/ )
154
174
155
175
## Enigma script
156
176
157
177
| ![ ] ( ../../images/module02/Cryptography__wiringdiagram.png ) |
158
- | :---------------: |
159
- | ** Figure 1.** Enigma machine diagram |
178
+ | :---: |
179
+ | ** Figure 1.** Enigma machine diagram |
160
180
161
181
``` ruby
162
182
Plugboard = Hash [* (' A' ..' Z' ).to_a.shuffle.first(20 )]
@@ -205,11 +225,10 @@ puts "Encrypted '#{plain_text}' to '#{encrypted = input(plain_text)}'"
205
225
puts " Decrypted '#{ encrypted } ' to '#{ decrypted = input(encrypted) } '"
206
226
puts ' Success!' if plain_text == decrypted
207
227
```
208
- [ Source | Understanding the Enigma machine with 30 lines of Ruby] [ 2 ]
209
-
210
228
229
+ [ Source \| Understanding the Enigma machine with 30 lines of Ruby] ( http://red-badger.com/blog/2015/02/23/understanding-the-enigma-machine-with-30-lines-of-ruby-star-of-the-2014-film-the-imitation-game )
211
230
212
231
---
213
- [ 1 ] : https://github.com/wimm/rubyntlm/blob/master/lib/net/ntlm.rb
214
- [ 2 ] : http://red-badger.com/blog/2015/02/23/understanding-the-enigma-machine-with-30-lines-of-ruby-star-of-the-2014-film-the-imitation-game
232
+
233
+
215
234
0 commit comments