Module: BlockIo::Helper
- Defined in:
- lib/block_io.rb
Class Method Summary collapse
-
.decrypt(encrypted_data, b64_enc_key, iv = nil, cipher_type = 'AES-256-ECB') ⇒ Object
Decrypts a block of data (encrypted_data) given an encryption key.
-
.encrypt(data, b64_enc_key, iv = nil, cipher_type = 'AES-256-ECB') ⇒ Object
Encrypts a block of data given an encryption key.
- .extractKey(encrypted_data, b64_enc_key) ⇒ Object
- .pinToAesKey(secret_pin, iterations = 2048) ⇒ Object
- .sha256(value) ⇒ Object
Class Method Details
.decrypt(encrypted_data, b64_enc_key, iv = nil, cipher_type = 'AES-256-ECB') ⇒ Object
Decrypts a block of data (encrypted_data) given an encryption key
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/block_io.rb', line 277 def self.decrypt(encrypted_data, b64_enc_key, iv = nil, cipher_type = 'AES-256-ECB') response = nil begin aes = OpenSSL::Cipher::Cipher.new(cipher_type) aes.decrypt aes.key = Base64.strict_decode64(b64_enc_key) aes.iv = iv if iv != nil response = aes.update(Base64.strict_decode64(encrypted_data)) + aes.final rescue Exception => e # decryption failed, must be an invalid Secret PIN raise Exception.new('Invalid Secret PIN provided.') end return response end |
.encrypt(data, b64_enc_key, iv = nil, cipher_type = 'AES-256-ECB') ⇒ Object
Encrypts a block of data given an encryption key
296 297 298 299 300 301 302 |
# File 'lib/block_io.rb', line 296 def self.encrypt(data, b64_enc_key, iv = nil, cipher_type = 'AES-256-ECB') aes = OpenSSL::Cipher::Cipher.new(cipher_type) aes.encrypt aes.key = Base64.strict_decode64(b64_enc_key) aes.iv = iv if iv != nil Base64.strict_encode64(aes.update(data) + aes.final) end |
.extractKey(encrypted_data, b64_enc_key) ⇒ Object
249 250 251 252 253 254 255 256 257 |
# File 'lib/block_io.rb', line 249 def self.extractKey(encrypted_data, b64_enc_key) # passphrase is in plain text # encrypted_data is in base64, as it was stored on Block.io # returns the private key extracted from the given encrypted data decrypted = self.decrypt(encrypted_data, b64_enc_key) return Key.from_passphrase(decrypted) end |
.pinToAesKey(secret_pin, iterations = 2048) ⇒ Object
266 267 268 269 270 271 272 273 274 |
# File 'lib/block_io.rb', line 266 def self.pinToAesKey(secret_pin, iterations = 2048) # converts the pincode string to PBKDF2 # returns a base64 version of PBKDF2 pincode salt = "" aes_key_bin = OpenSSL::PKCS5.pbkdf2_hmac(secret_pin, salt, iterations/2, 16, OpenSSL::Digest::SHA256.new) aes_key_bin = OpenSSL::PKCS5.pbkdf2_hmac(aes_key_bin.unpack("H*")[0], salt, iterations/2, 32, OpenSSL::Digest::SHA256.new) return Base64.strict_encode64(aes_key_bin) # the base64 encryption key end |
.sha256(value) ⇒ Object
259 260 261 262 263 264 |
# File 'lib/block_io.rb', line 259 def self.sha256(value) # returns the hex of the hash of the given value hash = Digest::SHA2.new(256) hash << value hash.hexdigest # return hex end |