Class: Blake2b::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/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.



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

def initialize(bytes)
  @bytes = bytes
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



5
6
7
# File 'lib/blake2b/key.rb', line 5

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



47
48
49
50
51
52
53
# File 'lib/blake2b/key.rb', line 47

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



34
35
36
37
38
39
40
# File 'lib/blake2b/key.rb', line 34

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



22
23
24
25
26
27
28
# File 'lib/blake2b/key.rb', line 22

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



14
15
16
# File 'lib/blake2b/key.rb', line 14

def self.none
  new([])
end