Class: RTunnel::Crypto::Hasher
- Inherits:
-
Object
- Object
- RTunnel::Crypto::Hasher
- Defined in:
- lib/rtunnel/crypto.rb
Overview
A cryptographically secure hasher. Instances will hash the data
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
-
.random_key ⇒ Object
Produces a random key for the hasher.
Instance Method Summary collapse
-
#hash(data) ⇒ Object
Creates a hash for the given data.
-
#initialize(key = nil) ⇒ Hasher
constructor
A new instance of Hasher.
Constructor Details
#initialize(key = nil) ⇒ Hasher
Returns a new instance of Hasher.
82 83 84 85 86 87 88 89 |
# File 'lib/rtunnel/crypto.rb', line 82 def initialize(key = nil) @key = key || RTunnel::Crypto::Hasher.random_key @cipher = OpenSSL::Cipher::Cipher.new 'aes-128-cbc' @cipher.encrypt iokey = StringIO.new @key @cipher.key = iokey.read_varstring @cipher.iv = iokey.read_varstring end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
80 81 82 |
# File 'lib/rtunnel/crypto.rb', line 80 def key @key end |
Class Method Details
.random_key ⇒ Object
Produces a random key for the hasher.
99 100 101 102 103 104 105 |
# File 'lib/rtunnel/crypto.rb', line 99 def self.random_key cipher = OpenSSL::Cipher::Cipher.new 'aes-128-cbc' iokey = StringIO.new iokey.write_varstring cipher.random_key iokey.write_varstring cipher.random_iv iokey.string end |
Instance Method Details
#hash(data) ⇒ Object
Creates a hash for the given data. Warning: this method is not idempotent. The intent is that the same hash can be produced by another hasher that is initialized with the same key and has been fed the same data.
94 95 96 |
# File 'lib/rtunnel/crypto.rb', line 94 def hash(data) @cipher.update Digest::SHA2.digest(data) end |