diff options
| author | Paul Buetow <paul@buetow.org> | 2023-09-14 10:20:37 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-09-14 10:20:37 +0300 |
| commit | e7ab2ef6c3f97fa1f3899d74e1108761c31eac94 (patch) | |
| tree | a02753961ae148e0f7f70c045f8f52b6eabda13a | |
| parent | c607fb2599fa399d104bc834d905840a0d553cfb (diff) | |
fix pin entry
| -rwxr-xr-x | geheim.rb | 47 |
1 files changed, 25 insertions, 22 deletions
@@ -134,39 +134,25 @@ end # Encryption functionality module Encryption include Log + @@key = nil def initialize super() - return unless @key.nil? + return unless @@key.nil? pin = read_pin # Set up initialization vector iv = "#{pin * 2}#{Config.add_to_iv}#{pin * 2}" - @iv = iv[0..15] + @@iv = iv[0..15] # ... and the encryption key! - @key = enforce_key_length(File.read(Config.key_file), Config.key_length) - end - - def enforce_key_length(key, force_size) - new_key = key - new_key += key while new_key.size < force_size - new_key[0..force_size - 1] - end - - def read_pin - return ENV['PIN'] if ENV['PIN'] - - prompt 'PIN: ' - return $stdin.gets.chomp if `uname`.include?('Android') - - $stdin.noecho(&:gets).chomp + @@key = enforce_key_length(File.read(Config.key_file), Config.key_length) end def encrypt(plain:) aes = OpenSSL::Cipher::Cipher.new(Config.enc_alg) aes.encrypt - aes.key = @key - aes.iv = @iv + aes.key = @@key + aes.iv = @@iv encrypted = aes.update(plain) encrypted << aes.final @@ -177,8 +163,8 @@ module Encryption def decrypt(encrypted:) aes = OpenSSL::Cipher::Cipher.new(Config.enc_alg) aes.decrypt - aes.key = @key - aes.iv = @iv + aes.key = @@key + aes.iv = @@iv plain = aes.update(encrypted) plain << aes.final @@ -191,6 +177,23 @@ module Encryption plain = decrypt(encrypted: encrypted) pp plain == plain_input end + + private + + def enforce_key_length(key, force_size) + new_key = key + new_key += key while new_key.size < force_size + new_key[0..force_size - 1] + end + + def read_pin + return ENV['PIN'] if ENV['PIN'] + + prompt 'PIN: ' + return $stdin.gets.chomp if `uname`.include?('Android') + + $stdin.noecho(&:gets).chomp + end end # Comitting a file |
