-
Notifications
You must be signed in to change notification settings - Fork 0
/
keygen3
57 lines (47 loc) · 1.6 KB
/
keygen3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env ruby
# Programatic interface to the RSA key generator for Chat 3.0.
# Writes keys to ruby file installed in ~/.sechat/address_book.rb
# Last revision: Dec 3, 2009
$LOAD_PATH.unshift File.dirname(__FILE__)
BITS = 2408
require "rsa.rb"
require 'fileutils'
require 'md5'
# File storage directory (user and server)
#FILE_DIRECTORY = (File.join(File.expand_path('~'), '.sechat') rescue '.')
FILE_DIRECTORY = '.'
# Make sure they specify a username
if ARGV.length != 1
$stderr.puts "Usage: #{$0} <username>"
Kernel.exit
end
# Make sure the directory exists
FileUtils::mkdir FILE_DIRECTORY rescue nil
# Warn if they're about to overwrite their key
if File.exists?("#{FILE_DIRECTORY}/address_book.rb")
raise "You already have a key in #{FILE_DIRECTORY}/address_book.rb"
end
# Make the key pair
$stderr.puts 'Making keys, this will take a while...'
pub, prv = Key.keygen(BITS)
fingerprint = []
MD5::digest(pub.to_s)[0,8].each_byte { |x| fingerprint << ("%02x" % x) }
fingerprint = fingerprint.join(' ')
# Output the keys to the rsa_keys file
begin
kf = File.new("#{FILE_DIRECTORY}/address_book.rb",
File::CREAT|File::TRUNC|File::WRONLY, 0600)
kf.puts("# Chat client RSA keys. KEEP THIS PRIVATE!\n\n")
kf.puts("RSA_ADDRESS_BOOK = {")
kf.puts(" :name => '#{ARGV[0]}',")
kf.puts(" :pub => '#{pub.to_s}',")
kf.puts(" :prv => '#{prv.to_s}',")
kf.puts("}")
kf.close
rescue
puts "Could not write to keyfile; aborting."
Kernel.exit
end
# Just print them to stdout
$stderr.puts "Completed! You can now run Chat 3.0."
$stderr.puts "\nYour key fingerprint is #{fingerprint}.\n\n"