Class: Digest::Blake2b::Key
- Inherits:
-
Object
- Object
- Digest::Blake2b::Key
- 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
-
#bytes ⇒ Object
readonly
Returns the value of attribute bytes.
Class Method Summary collapse
-
.from_bytes(bytes) ⇒ Blake2b::Key
Create a key from Array of Integer (0-255) Bytes.
-
.from_hex(str) ⇒ Blake2b::Key
Create a key from a Hex String [a-fA-F0-9].
-
.from_string(str) ⇒ Blake2b::Key
Create a key from an ASCII String.
-
.none ⇒ Blake2b::Key
Create a blank Key.
Instance Method Summary collapse
-
#initialize(bytes) ⇒ Key
constructor
A new instance of Key.
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
#bytes ⇒ Object (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.
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]
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
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 |
.none ⇒ Blake2b::Key
Create a blank Key
17 18 19 |
# File 'lib/digest/blake2b/key.rb', line 17 def self.none new([]) end |