Module: Encryption
- Defined in:
- lib/encryption.rb
Class Method Summary collapse
- .aes(m, t, k) ⇒ Object
- .aes_decrypt(text, key) ⇒ Object
- .aes_encrypt(text, key) ⇒ Object
- .blowfish_decrypt(text, key) ⇒ Object
- .blowfish_encrypt(text, key) ⇒ Object
- .decrypt_string(string) ⇒ Object
-
.encrypt_string(string) ⇒ Object
Blowfish, shorter 16 digit alphanumeric encryption based on http://www.rubyfleebie.com/encryption-with-alphanumeric-output/.
- .legacy_decrypt_string(encrypted_string) ⇒ Object
- .legacy_encrypt_string(string) ⇒ Object
- .url_decrypt_string(encrypted_string) ⇒ Object
- .url_encrypt_string(string) ⇒ Object
Class Method Details
.aes(m, t, k) ⇒ Object
95 96 97 98 |
# File 'lib/encryption.rb', line 95 def self.aes(m,t,k) (aes = OpenSSL::Cipher.new('aes-256-cbc').send(m)).key = Digest::SHA256.digest(k) aes.update(t) << aes.final end |
.aes_decrypt(text, key) ⇒ Object
90 91 92 |
# File 'lib/encryption.rb', line 90 def self.aes_decrypt(text, key) aes(:decrypt, text, key) rescue "" end |
.aes_encrypt(text, key) ⇒ Object
86 87 88 |
# File 'lib/encryption.rb', line 86 def self.aes_encrypt(text, key) aes(:encrypt, text, key) end |
.blowfish_decrypt(text, key) ⇒ Object
64 65 66 67 68 |
# File 'lib/encryption.rb', line 64 def self.blowfish_decrypt(text, key) require 'crypt/blowfish' blowfish = Crypt::Blowfish.new(key) blowfish.decrypt_block(text) rescue "" end |
.blowfish_encrypt(text, key) ⇒ Object
58 59 60 61 62 |
# File 'lib/encryption.rb', line 58 def self.blowfish_encrypt(text, key) require 'crypt/blowfish' blowfish = Crypt::Blowfish.new(key) blowfish.encrypt_block(text) end |
.decrypt_string(string) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/encryption.rb', line 32 def self.decrypt_string(string) # Rails.logger.debug "Encryption.decrypt_string, string: #{string}" str = string.to_s res = nil if str.length > 16 # for larger than 16 chars need to try legacy aes 256 which is long and has escaped characters res = self.url_decrypt_string(string) if res.blank? # this returns "" for strings that don't decrypt, that is string not encrypted using this method, so try legacy_decrypt_string res = self.legacy_decrypt_string(string) end elsif str.length == 16 # reverse the encrypt_string method, working on the encrypted string # convert back from 16 digit hex enc_str = str.each_line.to_a.pack("H*") # Rails.logger.debug "enc_str: #{enc_str}" # decrypt, this will yield an 8 digit string padded with null characters padded_string = self.blowfish_decrypt(enc_str, REST_AUTH_SITE_KEY) # Rails.logger.debug "padded_string: #{padded_string}" # remove null characters to get original string res = padded_string.gsub("\000","") end # Rails.logger.debug "res: #{res}" return res end |
.encrypt_string(string) ⇒ Object
Blowfish, shorter 16 digit alphanumeric encryption
based on http://www.rubyfleebie.com/encryption-with-alphanumeric-output/
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/encryption.rb', line 11 def self.encrypt_string(string) if string.to_s.length > 8 # for larger than 8 chars we will use url safe alphanumeric encryption self.url_encrypt_string(string) else # pad string to a multiple of 8 chars with null len = string.length mod = len == 8 ? 0 : 8-len%8 padded_string = string.ljust(len+mod, "\0") # Rails.logger.debug "padded_string: #{padded_string}" # Rails.logger.debug "padded_string.length: #{padded_string.length}" # then encrypt the string using two way blowfish enc_str =self.blowfish_encrypt(padded_string, REST_AUTH_SITE_KEY) # Rails.logger.debug "enc_str: #{enc_str}" # convert to 16 digit hex alphan_enc_str = enc_str.unpack("H*").first # Rails.logger.debug "alphan_enc_str: #{alphan_enc_str}" return alphan_enc_str end end |
.legacy_decrypt_string(encrypted_string) ⇒ Object
82 83 84 |
# File 'lib/encryption.rb', line 82 def self.legacy_decrypt_string(encrypted_string) self.aes_decrypt(Base64.decode64(CGI.unescape(encrypted_string)), REST_AUTH_SITE_KEY[0..31]) # first unescape, then Base64 decode, then decrypt with first 32 chars of the site key end |
.legacy_encrypt_string(string) ⇒ Object
78 79 80 |
# File 'lib/encryption.rb', line 78 def self.legacy_encrypt_string(string) CGI.escape(Base64.encode64(self.aes_encrypt(string.to_s, REST_AUTH_SITE_KEY[0..31]))) # need (256 bit = ) 32 char key so take first 32 chars of the site key which by necessity is stable, also need to Base64 encode and double URL encode/escape because it gets unescaped at the other end... all so that we can pass via http end |
.url_decrypt_string(encrypted_string) ⇒ Object
74 75 76 |
# File 'lib/encryption.rb', line 74 def self.url_decrypt_string(encrypted_string) self.aes_decrypt([encrypted_string].pack("H*"), REST_AUTH_SITE_KEY[0..31]) # first pack("H*") which un alphanumericizes it, then decrypt with first 32 chars of the site key end |
.url_encrypt_string(string) ⇒ Object
70 71 72 |
# File 'lib/encryption.rb', line 70 def self.url_encrypt_string(string) (self.aes_encrypt(string.to_s, REST_AUTH_SITE_KEY[0..31])).unpack("H*").first # need (256 bit = ) 32 char key so take first 32 chars of the site key which by necessity is stable, also need to make it alphanumeric using unpack("H*") so that we can pass via http end |