Module: GpgCrypt

Defined in:
lib/gpgcrypt.rb,
lib/gpgcrypt/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert_openssh_public_key_to_openssl(public_key_string, check_key_type = true) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gpgcrypt.rb', line 100

def self.convert_openssh_public_key_to_openssl(public_key_string, check_key_type = true)
  return public_key_string if get_key_type?(public_key_string) == :public
  
  # return public_key_string if true
  temp_file_path = '/tmp/gpgcrypt_pubtest'
  File.open(temp_file_path, "w+") do |f|
    f.puts public_key_string
  end
  File.chmod(0600, temp_file_path)
  
  # binding.pry
  #openssl_key = `openssl rsa -in #{temp_file_path} -out pub.der -outform pem -pubout`
  openssl_key = `ssh-keygen -f #{temp_file_path}  -e -m pem`
  File.delete(temp_file_path)
  return openssl_key
end

.decrypt(encrypted_message, private_key) ⇒ string

Returns decrypted message.

Parameters:

  • encrypted (string)

    message

  • public (string)

    key in PEM format

Returns:

  • (string)

    decrypted message



58
59
60
61
62
63
64
65
# File 'lib/gpgcrypt.rb', line 58

def self.decrypt(encrypted_message, private_key)
  message_string = encrypted_message
  private_key_string = private_key
  
  # decrypt the message
  dec_cipher = Gibberish::RSA.new(private_key_string)
  decrypted_message = dec_cipher.decrypt(message_string)
end

.encrypt(message, public_key) ⇒ string

Returns encrypted message.

Parameters:

  • message (string)
  • public_key (string)

Returns:

  • (string)

    encrypted message



43
44
45
46
47
48
49
50
# File 'lib/gpgcrypt.rb', line 43

def self.encrypt(message, public_key)
  message_string = message
  public_key_string = public_key
  
  # encrypt the message
  cipher = Gibberish::RSA.new(public_key_string)
  encrypted_message = cipher.encrypt(message_string)
end

.encrypt_or_decrypt(*args) ⇒ Object



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
# File 'lib/gpgcrypt.rb', line 10

def self.encrypt_or_decrypt(*args)
  key_data = File.read(args[1]) #if File.exists
  key_type = get_key_type?(key_data)  # args[1] is either a public key or a private key...
  
  # convert_key_to_pem_format
  if key_type == :open_ssh
    key_data = convert_openssh_public_key_to_openssl(key_data)
    key_type = get_key_type?(key_data)    # if this fails, we should set key_type to :incompatible
  end
  
  # get_message_data  decide where to read a file as the message, or use the first param
  if File.exist? args[0]
    message_data = File.read(args[0])
  else
    message_data = args[0]
  end
  
  case key_type
  when :public
    encrypt(message_data, key_data)
  when :private
    decrypt(message_data, key_data)
  else  # :incompatible or even possibly :open_ssh if something odd happens
    puts "The key entered was niether a public nor private key.  Check that you passed in a proper path to a key as the second parameter."
  end
end

.export_public_key(private_key) ⇒ Object

This command let’s you read your private key and generate from that a suitable public key which will be posted to pastebin.



69
70
71
# File 'lib/gpgcrypt.rb', line 69

def self.export_public_key(private_key)
  raise "stub: not yet implemented"
end

.get_key_type?(string) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gpgcrypt.rb', line 118

def self.get_key_type?(string)
  valid_private_key = :incompatible
  
  return :open_ssh if string =~ /^ssh-rsa/
  
  begin
    k = OpenSSL::PKey::RSA.new(string)
    if k.private?
      valid_private_key = :private
    elsif k.public?
      valid_private_key = :public
    else
      valid_private_key = :incompatible
    end
  rescue
    valid_private_key = :incompatible
  end
  valid_private_key
end

.helpObject



73
74
75
76
77
78
79
80
# File 'lib/gpgcrypt.rb', line 73

def self.help
  hlp = "gpgcrypt allows you to 'manually' encrypt and decrypt messages over the command line.\n\n"
  hlp += "To encrypt a message: \n  Supply a path to a file containing a message and an openSSL style public key. \n\n"
  hlp += "To decrypt a message: \n  Supply a path to a file containing an encrypted message and a private key.\n\n"
  hlp += "Soon you'll be able to export a public key from your private key and automatically pastebin it so others can message you.\n\n"
  hlp += "As an added feature, someday you will be able to specify pastebin links to encrypted messages and public keys"
  hlp
end

.path_to_content(string) ⇒ Object

checks if a string is a path to a file or a URL Returns a string of the file or URL page contents



85
86
87
88
89
90
91
92
93
# File 'lib/gpgcrypt.rb', line 85

def self.path_to_content(string)
  string_is_a_file = File.exists? string
  string_is_a_uri = string =~ URI::regexp
  if string_is_a_file
    File.read(string)
  elsif string_is_a_uri
    URI.parse(string).read
  end
end

Instance Method Details

#scan_if_encrypted_message(string) ⇒ Object



95
96
97
98
# File 'lib/gpgcrypt.rb', line 95

def scan_if_encrypted_message(string)
  return true if is_string_private_key?
  false
end