Class: RecordOnChain::Crypto::Cryptor
- Inherits:
-
Object
- Object
- RecordOnChain::Crypto::Cryptor
- Defined in:
- lib/record_on_chain/crypto/cryptor.rb
Instance Method Summary collapse
- #decrypt(passwd, hex_salt, hex_encrypted_data) ⇒ Object
- #encrypt(passwd, hex_salt, hex_secret) ⇒ Object
- #generate_salt ⇒ Object
- #generate_secret ⇒ Object
-
#initialize(crypto_engine) ⇒ Cryptor
constructor
A new instance of Cryptor.
Constructor Details
#initialize(crypto_engine) ⇒ Cryptor
Returns a new instance of Cryptor.
8 9 10 |
# File 'lib/record_on_chain/crypto/cryptor.rb', line 8 def initialize( crypto_engine ) @crypto_engine = crypto_engine end |
Instance Method Details
#decrypt(passwd, hex_salt, hex_encrypted_data) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/record_on_chain/crypto/cryptor.rb', line 29 def decrypt( passwd, hex_salt, hex_encrypted_data ) b_data = Utils.hex_to_bytes( hex_encrypted_data ) b_salt = Utils.hex_to_bytes( hex_salt ) decrypted = "" begin # At this point it is not known whether the decrypted data is correct or not. # You should verify decrypted data with checksum. decrypted = @crypto_engine.decrypt( passwd, b_salt, b_data ) rescue OpenSSL::Cipher::CipherError # fail to encrypt return "" end # validate checksum secret_length = decrypted.size - CHECKSUM_LENGTH secret_part = decrypted[0,secret_length] checksum_part = decrypted[secret_length..-1] checksum = calc_checksum( secret_part ) # not match checksum return "" unless checksum_part == checksum # match checksum return Utils.bytes_to_hex( secret_part ) end |
#encrypt(passwd, hex_salt, hex_secret) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/record_on_chain/crypto/cryptor.rb', line 20 def encrypt( passwd, hex_salt, hex_secret ) b_secret = Utils.hex_to_bytes( hex_secret ) # add checksum to secret b_secret << calc_checksum( b_secret ) b_salt = Utils.hex_to_bytes( hex_salt ) encrypted = @crypto_engine.encrypt( passwd, b_salt, b_secret ) return Utils.bytes_to_hex( encrypted ) end |
#generate_salt ⇒ Object
16 17 18 |
# File 'lib/record_on_chain/crypto/cryptor.rb', line 16 def generate_salt return SecureRandom.hex( SALT_LENGTH ) end |
#generate_secret ⇒ Object
12 13 14 |
# File 'lib/record_on_chain/crypto/cryptor.rb', line 12 def generate_secret return SecureRandom.hex( SECRET_LENGTH ) end |