Module: DropboxExt::EncryptionHelper

Defined in:
app/helpers/dropbox_ext/encryption_helper.rb

Constant Summary collapse

ALG =
"AES-256-CBC"
DEFAULT_COST =
11

Class Method Summary collapse

Class Method Details

.decrypt(cipher64, key) ⇒ Object



25
26
27
28
29
30
31
32
# File 'app/helpers/dropbox_ext/encryption_helper.rb', line 25

def self.decrypt(cipher64, key)
  decode_cipher = OpenSSL::Cipher::Cipher.new(ALG)
  decode_cipher.decrypt
  decode_cipher.key = [key].pack('H*')
  plain = decode_cipher.update(cipher64.unpack('m')[0])
  plain << decode_cipher.final
  plain
end

.encrypt(message, key) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'app/helpers/dropbox_ext/encryption_helper.rb', line 15

def self.encrypt(message, key)
  aes = OpenSSL::Cipher::Cipher.new(ALG)
  aes.encrypt
  aes.key = [key].pack('H*')
  cipher = aes.update(message)
  cipher << aes.final
  cipher64 = [cipher].pack('m')
  cipher64
end

.generate_random_keyObject



11
12
13
# File 'app/helpers/dropbox_ext/encryption_helper.rb', line 11

def self.generate_random_key
  Digest::SHA256.hexdigest(SecureRandom.hex)
end

.hash_password(password) ⇒ Object



42
43
44
# File 'app/helpers/dropbox_ext/encryption_helper.rb', line 42

def self.hash_password(password)
  BCrypt::Password.create(password, cost: DEFAULT_COST).to_s
end

.sha256(string) ⇒ Object



34
35
36
# File 'app/helpers/dropbox_ext/encryption_helper.rb', line 34

def self.sha256(string)
  Digest::SHA256.hexdigest(string)
end

.test_password(password, hash) ⇒ Object



46
47
48
49
50
# File 'app/helpers/dropbox_ext/encryption_helper.rb', line 46

def self.test_password(password, hash)
  bcrypt = BCrypt::Password.new(hash)
  password = BCrypt::Engine.hash_secret(password, bcrypt.salt)
  return password == hash
end