Class: Encrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/vimamsa/encrypt.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pass_phrase) ⇒ Encrypt

Returns a new instance of Encrypt.



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

def initialize(pass_phrase)
  salt = "uvgixEtU"
  @enc = OpenSSL::Cipher.new "AES-128-CBC"
  @enc.encrypt
  @enc.pkcs5_keyivgen pass_phrase, salt
  @dec = OpenSSL::Cipher.new "AES-128-CBC"
  @dec.decrypt
  @dec.pkcs5_keyivgen pass_phrase, salt
end

Class Method Details

.is_encrypted?(fn) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vimamsa/encrypt.rb', line 11

def self.is_encrypted?(fn)
  debug "self.is_encrypted?(fn)", 2
  begin
    file = File.open(fn, "r")
    first_11_characters = file.read(11)
    return true if first_11_characters == "VMACRYPT001"
  rescue Errno::ENOENT
    puts "File not found: #{file_path}"
  rescue => e
    puts "An error occurred: #{e.message}"
  ensure
    file&.close
  end
  return false
end

.open(fn, password) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vimamsa/encrypt.rb', line 27

def self.open(fn, password)
  debug "open_encrypted(filename,password)", 2
  encrypted = read_file("", fn)[11..-1]
  begin
    crypt = Encrypt.new(password)
    str = crypt.decrypt(encrypted)
    # debug "PASS OK!", 2
    bu = create_new_buffer(str)
    bu.init_encrypted(crypt: crypt, filename: fn, encrypted: encrypted)
  rescue OpenSSL::Cipher::CipherError => e
    # Wrong password
    decrypt_dialog(filename: fn, wrong_pass: true)
  end
end

Instance Method Details

#decrypt(encrypted) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/vimamsa/encrypt.rb', line 61

def decrypt(encrypted)
  cipher = @dec
  encrypted = [encrypted].pack("H*").unpack("C*").pack("c*")
  plain = cipher.update encrypted
  plain << cipher.final
  plain.force_encoding("utf-8")
  @dec.reset
  return plain
end

#encrypt(text) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/vimamsa/encrypt.rb', line 52

def encrypt(text)
  cipher = @enc
  encrypted = cipher.update text
  encrypted << cipher.final
  encrypted = encrypted.unpack("H*")[0].upcase
  @enc.reset
  return encrypted
end