Class: Crypto
- Inherits:
-
Object
- Object
- Crypto
- Defined in:
- lib/myer/crypto.rb
Instance Attribute Summary collapse
-
#passphrase ⇒ Object
Returns the value of attribute passphrase.
Instance Method Summary collapse
- #call_cmd(cmd, input) ⇒ Object
- #decrypt(ciphertext) ⇒ Object
- #encrypt(plaintext) ⇒ Object
- #generate_passphrase ⇒ Object
Instance Attribute Details
#passphrase ⇒ Object
Returns the value of attribute passphrase.
3 4 5 |
# File 'lib/myer/crypto.rb', line 3 def passphrase @passphrase end |
Instance Method Details
#call_cmd(cmd, input) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/myer/crypto.rb', line 9 def call_cmd(cmd, input) output = nil Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr| stdin.puts(input) stdin.close output = stdout.read if !wait_thr.value.success? raise Myer::CmdFailed.new(stderr.read) end end output end |
#decrypt(ciphertext) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/myer/crypto.rb', line 32 def decrypt(ciphertext) cmd = "gpg --batch --passphrase '#{passphrase}' --decrypt" begin return call_cmd(cmd, ciphertext).chomp rescue Myer::CmdFailed => e raise Myer::DecryptionFailed.new("Decryption failed: #{e}") end end |
#encrypt(plaintext) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/myer/crypto.rb', line 23 def encrypt(plaintext) cmd = "gpg --batch --armor --passphrase '#{passphrase}' --symmetric" begin return call_cmd(cmd, plaintext) rescue Myer::CmdFailed => e raise "Encryption failed: #{e}" end end |
#generate_passphrase ⇒ Object
5 6 7 |
# File 'lib/myer/crypto.rb', line 5 def generate_passphrase `gpg --armor --gen-random 1 16`.chomp end |