module Shamu::Security::HashedValue
Adds support for hashing and verifying a string value.
“`ruby class Codec
include Shamu::Security::HashedValue def initialize( private_key = Shamu::Security.private_key ) @private_key = private_key end def store( value ) hash_value( value ) end def restore( hashed ) verify_hash( hashed ) end
end
codec = Codec.new signed = codec.store “example” # => “0123456789abcdef0123456789abcdef012345678;example” codec.restore signed # => “example” codec.restore “example” # => nil “`
Attributes
private_key[R]
@!visiblity public @return [String] the private key used to sign the hashes.
Private Instance Methods
hash_digest( string )
click to toggle source
# File lib/shamu/security/hashed_value.rb, line 47 def hash_digest( string ) alg = OpenSSL::Digest::SHA1.new OpenSSL::HMAC.hexdigest( alg, private_key, string ) end
hash_value( string )
click to toggle source
@!visibility public
@param [String] string to hash. @return [String] packed string with hash and original value.
# File lib/shamu/security/hashed_value.rb, line 42 def hash_value( string ) return nil unless string "#{ hash_digest( string ) }$#{ string }" end
verify_hash( hashed )
click to toggle source
@!visiblity public
Verify that the hashed value has not been modified.
@param [String] hashed value returned from {#hash_value}. @return [String] the original value.
# File lib/shamu/security/hashed_value.rb, line 58 def verify_hash( hashed ) return unless hashed return if hashed.length < 41 mac = hashed[ 0...40 ] toggles = hashed[ 41..-1 ] return unless hash_digest( toggles ) == mac toggles end