Class: Digest::Blake2b::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/digest/blake2b/key.rb

Overview

Validate and normalize an HMAC key, provided in different formats, into an Array of Integer Bytes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ Key

Returns a new instance of Key.



10
11
12
# File 'lib/digest/blake2b/key.rb', line 10

def initialize(bytes)
  @bytes = bytes
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



8
9
10
# File 'lib/digest/blake2b/key.rb', line 8

def bytes
  @bytes
end

Class Method Details

.from_bytes(bytes) ⇒ Blake2b::Key

Create a key from Array of Integer (0-255) Bytes. This simply validates and passes through the Array.

Parameters:

  • str (Array)

    an Array of Integer (0-255) Bytes

Returns:

  • (Blake2b::Key)

    a Blake2b::Key object with a ‘bytes` attr



50
51
52
53
54
55
56
# File 'lib/digest/blake2b/key.rb', line 50

def self.from_bytes(bytes)
  if bytes.all? { |b| b.is_a?(Integer) && b.between?(0, 255) }
    new(bytes)
  else
    raise ArgumentError, 'key must be a Byte Array of Integers (0-255)'
  end
end

.from_hex(str) ⇒ Blake2b::Key

Create a key from a Hex String [a-fA-F0-9]

Parameters:

  • str (String)

    a Hex String key

Returns:

  • (Blake2b::Key)

    a Blake2b::Key object with a ‘bytes` attr



37
38
39
40
41
42
43
# File 'lib/digest/blake2b/key.rb', line 37

def self.from_hex(str)
  if str.is_a?(String) && str.match(/^[a-fA-F0-9]+$/)
    new([str].pack('H*').bytes)
  else
    raise ArgumentError, 'key must be a Hex String [a-fA-F0-9]'
  end
end

.from_string(str) ⇒ Blake2b::Key

Create a key from an ASCII String

Parameters:

  • str (String)

    an ASCII String key

Returns:

  • (Blake2b::Key)

    a Blake2b::Key object with a ‘bytes` attr



25
26
27
28
29
30
31
# File 'lib/digest/blake2b/key.rb', line 25

def self.from_string(str)
  if str.is_a?(String) && str.ascii_only?
    new(str.bytes)
  else
    raise ArgumentError, 'key must be an ASCII String'
  end
end

.noneBlake2b::Key

Create a blank Key

Returns:

  • (Blake2b::Key)

    a Blake2b::Key object with a ‘bytes` attr



17
18
19
# File 'lib/digest/blake2b/key.rb', line 17

def self.none
  new([])
end