Class: Blake2b::Key
- Inherits:
-
Object
- Object
- Blake2b::Key
- 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
-
#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.
7 8 9 |
# File 'lib/blake2b/key.rb', line 7 def initialize(bytes) @bytes = bytes end |
Instance Attribute Details
#bytes ⇒ Object (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.
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]
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
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 |
.none ⇒ Blake2b::Key
Create a blank Key
14 15 16 |
# File 'lib/blake2b/key.rb', line 14 def self.none new([]) end |