Module: Noteshred::Crypto::V4
- Defined in:
- lib/noteshred/crypto.rb
Constant Summary collapse
- ITERATIONS =
20000
Class Method Summary collapse
-
.decrypt(content, pass, salt, iv) ⇒ Object
Expects UTF-8 encoded strings from the encrypt method.
-
.encrypt(content, pass) ⇒ Object
Outputs UTF-8 encoded object for storing in database.
- .hash(pass, salt) ⇒ Object
Class Method Details
.decrypt(content, pass, salt, iv) ⇒ Object
Expects UTF-8 encoded strings from the encrypt method
29 30 31 32 33 34 35 36 37 |
# File 'lib/noteshred/crypto.rb', line 29 def self.decrypt(content,pass,salt,iv) content = Noteshred::Tools.decode_utf8(content) cipher = OpenSSL::Cipher.new('AES-256-CBC') cipher.decrypt cipher.iv = Noteshred::Tools.decode_utf8(iv) cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(pass, salt, ITERATIONS, cipher.key_len) result = cipher.update(content) result << cipher.final end |
.encrypt(content, pass) ⇒ Object
Outputs UTF-8 encoded object for storing in database
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/noteshred/crypto.rb', line 11 def self.encrypt(content,pass) raise ArgumentError, 'Content and password required' if content.empty? || pass.empty? cipher = OpenSSL::Cipher.new('AES-256-CBC') cipher.encrypt iv = cipher.random_iv salt = SecureRandom.hex(16) cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(pass, salt, ITERATIONS, cipher.key_len) result = cipher.update(content) result << cipher.final return { :content => Noteshred::Tools.encode_utf8(result), :iv => Noteshred::Tools.encode_utf8(iv), :salt => salt, :version => 4 } end |
.hash(pass, salt) ⇒ Object
39 40 41 |
# File 'lib/noteshred/crypto.rb', line 39 def self.hash(pass,salt) return OpenSSL::PKCS5::pbkdf2_hmac_sha1(pass, salt, ITERATIONS, 32) end |