Module: BSONMiniHash::Hex
- Defined in:
- lib/bson_minihash/hex.rb
Constant Summary collapse
- FORMAT =
'H*'.freeze
- TYPES =
[:md5, :sha1, :hex].freeze
Class Method Summary collapse
- .digest(data, type) ⇒ Object
- .pack(str, type = :hex) ⇒ Object
- .unpack(packed_str) ⇒ Object
- .validate(str, type) ⇒ Object
Class Method Details
.digest(data, type) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/bson_minihash/hex.rb', line 29 def self.digest(data, type) case type when :md5 Digest::MD5.digest(data) when :sha1 Digest::SHA1.digest(data) else raise ArgumentError, "type #{type} not supported" end end |
.pack(str, type = :hex) ⇒ Object
8 9 10 11 |
# File 'lib/bson_minihash/hex.rb', line 8 def self.pack(str, type = :hex) Hex.validate(str, type) [str].pack(FORMAT) end |
.unpack(packed_str) ⇒ Object
13 14 15 |
# File 'lib/bson_minihash/hex.rb', line 13 def self.unpack(packed_str) packed_str.unpack(FORMAT).first end |
.validate(str, type) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/bson_minihash/hex.rb', line 17 def self.validate(str, type) raise ArgumentError, "Hex strings must comprise of characters between a-f and 0-9" unless str.match /^[a-f0-9]+$/i case type when :md5 raise ArgumentError, "MD5 strings must be 32 characters in length" unless str.length == 32 when :sha1 raise ArgumentError, "SHA1 strings must be 40 characters in length" unless str.length == 40 end true end |