Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ext/string.rb

Instance Method Summary collapse

Instance Method Details

#decrypt(key = "Some random key") ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ext/string.rb', line 25

def decrypt(key = "Some random key")
  blowfish = Kruptos::Blowfish.new(key)
  value = ::Base64.decode64(self)
  
  decryptedValue = ""
  (
    chunk = value.slice!(0,8)
    decryptedValue += blowfish.decrypt_block(chunk)
  ) until value.empty?
  
  (
    decryptedValue = decryptedValue.chomp("\012")
  ) until (decryptedValue[decryptedValue.length-1] != "\012")

  return decryptedValue
end

#encrypt(key = "Some random key") ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ext/string.rb', line 7

def encrypt(key = "Some random key")
  blowfish = Kruptos::Blowfish.new(key)
  
  String val = self

  # pad the value to be 8 bytes aligned.
  (8-(val.length % 8)).times { val += "\012" }
  encodedString = Base64.encode64(val)
  
  encryptedValue = ""
  (
    chunk = val.slice!(0,8)
    encryptedValue += blowfish.encrypt_block(chunk)
  ) until val.empty?
  
  return ::Base64.encode64(encryptedValue);
end