Skip to content

Instantly share code, notes, and snippets.

@rbmrclo
Last active August 29, 2015 14:18
Show Gist options
  • Save rbmrclo/e4df82f1a8f04b11a325 to your computer and use it in GitHub Desktop.
Save rbmrclo/e4df82f1a8f04b11a325 to your computer and use it in GitHub Desktop.
SHA1Generator
require 'digest/sha1'
#
# A wrapper for generating SHA1 hash value
# Simply done by concatenating all values using the colon symbol for delimiter.
# This algorithm is mostly used by payment gateways.
#
# Usage:
#
# SHA1Generator.digest('foo', 'bar', 'fizz', 'buzz')
# => "e723e86a6000fc8462142eb4821672de107535c1" # 40 chars
#
# hash_value = SHA1Generator.new('foo', 'bar', 'fizz', 'buzz')
# hash_value.digestible_attributes
# => "foo:bar:fizz:buzz"
#
# hash_value.digest
# => "e723e86a6000fc8462142eb4821672de107535c1" # 40 chars
#
class SHA1Generator
def self.digest(*attributes)
new(*attributes).digest
end
def initialize(*attributes)
@attrs = attributes
end
def digestible_attributes
@attrs * ":"
end
def digest
Digest::SHA1.hexdigest(digestible_attributes)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment