sshkey 1.3.3 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sshkey.rb CHANGED
@@ -193,6 +193,63 @@ class SSHKey
193
193
  key_object.to_text.match(/Private-Key:\s\((\d*)\sbit\)/)[1].to_i
194
194
  end
195
195
 
196
+ # Randomart
197
+ #
198
+ # Generate OpenSSH compatible ASCII art fingerprints
199
+ # See http://www.opensource.apple.com/source/OpenSSH/OpenSSH-175/openssh/key.c (key_fingerprint_randomart function)
200
+ #
201
+ # Example:
202
+ # +--[ RSA 2048]----+
203
+ # |o+ o.. |
204
+ # |..+.o |
205
+ # | ooo |
206
+ # |.++. o |
207
+ # |+o+ + S |
208
+ # |.. + o . |
209
+ # | . + . |
210
+ # | . . |
211
+ # | Eo. |
212
+ # +-----------------+
213
+ def randomart
214
+ fieldsize_x = 17
215
+ fieldsize_y = 9
216
+ x = fieldsize_x / 2
217
+ y = fieldsize_y / 2
218
+ raw_digest = Digest::MD5.digest(ssh_public_key_conversion)
219
+ num_bytes = raw_digest.bytesize
220
+
221
+ field = Array.new(fieldsize_x) { Array.new(fieldsize_y) {0} }
222
+
223
+ raw_digest.bytes.each do |byte|
224
+ 4.times do
225
+ x += (byte & 0x1 != 0) ? 1 : -1
226
+ y += (byte & 0x2 != 0) ? 1 : -1
227
+
228
+ x = [[x, 0].max, fieldsize_x - 1].min
229
+ y = [[y, 0].max, fieldsize_y - 1].min
230
+
231
+ field[x][y] += 1 if (field[x][y] < num_bytes - 2)
232
+
233
+ byte >>= 2
234
+ end
235
+ end
236
+
237
+ field[fieldsize_x / 2][fieldsize_y / 2] = num_bytes - 1
238
+ field[x][y] = num_bytes
239
+ augmentation_string = " .o+=*BOX@%&#/^SE"
240
+ output = "+--#{sprintf("[%4s %4u]", type.upcase, bits)}----+\n"
241
+ fieldsize_y.times do |y|
242
+ output << "|"
243
+ fieldsize_x.times do |x|
244
+ output << augmentation_string[[field[x][y], num_bytes].min]
245
+ end
246
+ output << "|"
247
+ output << "\n"
248
+ end
249
+ output << "+#{"-" * fieldsize_x}+"
250
+ output
251
+ end
252
+
196
253
  private
197
254
 
198
255
  # SSH Public Key Conversion
@@ -1,3 +1,3 @@
1
1
  class SSHKey
2
- VERSION = "1.3.3"
2
+ VERSION = "1.4.0"
3
3
  end
data/test/sshkey_test.rb CHANGED
@@ -87,6 +87,48 @@ EOF
87
87
  KEY2_SHA1_FINGERPRINT = "9a:52:78:2b:6b:cb:39:b7:85:ed:90:8a:28:62:aa:b3:98:88:e6:07"
88
88
  KEY3_SHA1_FINGERPRINT = "15:68:c6:72:ac:18:d1:fc:ab:a2:b7:b5:8c:d1:fe:8f:b9:ae:a9:47"
89
89
 
90
+ KEY1_RANDOMART = <<-EOF.rstrip
91
+ +--[ RSA 2048]----+
92
+ |o+ o.. |
93
+ |..+.o |
94
+ | ooo |
95
+ |.++. o |
96
+ |+o+ + S |
97
+ |.. + o . |
98
+ | . + . |
99
+ | . . |
100
+ | Eo. |
101
+ +-----------------+
102
+ EOF
103
+
104
+ KEY2_RANDOMART = <<-EOF.rstrip
105
+ +--[ RSA 2048]----+
106
+ | ..o.. |
107
+ | ..+ . |
108
+ | o . |
109
+ | . o |
110
+ | . o S . |
111
+ | + o O o |
112
+ | + + O . |
113
+ | = o . |
114
+ | .E |
115
+ +-----------------+
116
+ EOF
117
+
118
+ KEY3_RANDOMART = <<-EOF.rstrip
119
+ +--[ DSA 1024]----+
120
+ | .=o. |
121
+ | .+.o . |
122
+ | + =.o . . |
123
+ | + * + . . |
124
+ | + = S . E |
125
+ | + = . . |
126
+ | . |
127
+ | |
128
+ | |
129
+ +-----------------+
130
+ EOF
131
+
90
132
  def setup
91
133
  @key1 = SSHKey.new(SSH_PRIVATE_KEY1, :comment => "me@example.com")
92
134
  @key2 = SSHKey.new(SSH_PRIVATE_KEY2, :comment => "me@example.com")
@@ -224,7 +266,13 @@ EOF
224
266
  assert_equal 2048, @key1.bits
225
267
  assert_equal 2048, @key2.bits
226
268
  assert_equal 1024, @key3.bits
227
- assert_equal 512, SSHKey.generate(bits: 512).bits
269
+ assert_equal 512, SSHKey.generate(:bits => 512).bits
270
+ end
271
+
272
+ def test_randomart
273
+ assert_equal KEY1_RANDOMART, @key1.randomart
274
+ assert_equal KEY2_RANDOMART, @key2.randomart
275
+ assert_equal KEY3_RANDOMART, @key3.randomart
228
276
  end
229
277
 
230
278
  def test_to_byte_array
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sshkey
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3
4
+ version: 1.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: