Class: CBOR_DIAG::App_hash

Inherits:
Object show all
Defined in:
lib/cbor-diagnostic-app/hash.rb

Constant Summary collapse

HASHES =
{
  -14 => Digest::SHA1,
  "SHA-1" => Digest::SHA1,
  -16 => Digest::SHA256,
  "SHA-256" => Digest::SHA256,
  -43 => Digest::SHA384,
  "SHA-384" => Digest::SHA384,
  -44 => Digest::SHA512,
  "SHA-512" => Digest::SHA512,
}

Class Method Summary collapse

Class Method Details

.decode(_, s) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cbor-diagnostic-app/hash.rb', line 15

def self.decode(_, s)
  if CBOR::Sequence === s
    args = s.elements
  else
    args = [s]
  end
  case args
  in [String]
    args[1] = -16             # default SHA-256
  in [String, Integer | String]
  else
    raise ArgumentError.new("cbor-diagnostic: hash<< #{args.inspect[1...-1]} >>: Argument Error")
  end
  fn = HASHES[args[1]]
  if fn
    fn.digest(args[0])
  else
    raise ArgumentError.new("cbor-diagnostic: hash<<>>: unimplemented hash function #{args[1]}")
  end
end