class OpenToken::Cipher
Constants
- AES_128_CBC
- AES_256_CBC
- DES3_168_CBC
- NULL
- REGISTERED_CIPHERS
Attributes
algorithm[R]
iv_length[R]
key_length[R]
suite[R]
Public Class Methods
for_suite(cipher_suite)
click to toggle source
# File lib/opentoken/cipher.rb, line 18 def self.for_suite(cipher_suite) cipher = REGISTERED_CIPHERS.detect {|c| c.suite == cipher_suite } raise InvalidCipherError.new("Unknown cipher suite: #{cipher_suite}") unless cipher cipher end
new(attrs = {})
click to toggle source
# File lib/opentoken/cipher.rb, line 12 def initialize(attrs = {}) @suite = attrs[:suite] @iv_length = attrs[:iv_length] @key_length = attrs[:key_length] @algorithm = attrs[:algorithm] end
Public Instance Methods
decrypt_payload(encrypted_payload, key, iv)
click to toggle source
see snippets.dzone.com/posts/show/4975 see jdwyah.blogspot.com/2009/12/decrypting-ruby-aes-encryption.html see snippets.dzone.com/posts/show/576
# File lib/opentoken/cipher.rb, line 34 def decrypt_payload(encrypted_payload, key, iv) return encrypted_payload unless algorithm c = crypt :decrypt, key, iv c.update(encrypted_payload) + c.final end
encrypt_payload(payload, key, iv)
click to toggle source
# File lib/opentoken/cipher.rb, line 39 def encrypt_payload(payload, key, iv) c = crypt :encrypt, key, iv padding = if payload.length % iv_length == 0 iv_length else iv_length - (payload.length % iv_length) end c.update(payload + (padding.chr * padding)) end
generate_iv()
click to toggle source
# File lib/opentoken/cipher.rb, line 27 def generate_iv OpenSSL::Random.random_bytes(iv_length) end
generate_key()
click to toggle source
# File lib/opentoken/cipher.rb, line 24 def generate_key OpenToken::PasswordKeyGenerator.generate OpenToken.password, self end
Private Instance Methods
crypt(operation, key, iv)
click to toggle source
# File lib/opentoken/cipher.rb, line 50 def crypt(operation, key, iv) crypt = OpenSSL::Cipher::Cipher.new(algorithm) crypt.send operation crypt.key = key crypt.iv = iv crypt end