Class: Keycard::DigestKey
- Inherits:
-
Object
- Object
- Keycard::DigestKey
- Defined in:
- lib/keycard/digest_key.rb
Overview
A typical digest or api key, ready to be encrypted.
Defined Under Namespace
Classes: HiddenKeyError
Constant Summary collapse
- HIDDEN_KEY =
"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX".freeze
Instance Method Summary collapse
-
#digest ⇒ String
The result of hashing the key.
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(digest = nil, key: nil) ⇒ DigestKey
constructor
To simply mint a new key, call #new without any parameters.
-
#to_s ⇒ String
A string representation of this key.
-
#value ⇒ String
The unhashed value of the key.
Constructor Details
#initialize(digest = nil, key: nil) ⇒ DigestKey
To simply mint a new key, call #new without any parameters. For wrapping existing, deserialized keys, pass the digest to the constructor.
16 17 18 19 20 21 22 |
# File 'lib/keycard/digest_key.rb', line 16 def initialize(digest = nil, key: nil) if digest @digest = digest else @key = key || SecureRandom.uuid end end |
Instance Method Details
#digest ⇒ String
The result of hashing the key
45 46 47 |
# File 'lib/keycard/digest_key.rb', line 45 def digest @digest ||= Digest::SHA256.hexdigest(@key) end |
#eql?(other) ⇒ Boolean Also known as: ==
49 50 51 52 53 54 55 |
# File 'lib/keycard/digest_key.rb', line 49 def eql?(other) digest == if other.is_a?(self.class) other.digest else other.to_s end end |
#to_s ⇒ String
A string representation of this key. For hidden keys, this returns an obfuscated value.
27 28 29 |
# File 'lib/keycard/digest_key.rb', line 27 def to_s @key || HIDDEN_KEY end |
#value ⇒ String
The unhashed value of the key.
35 36 37 38 39 40 41 |
# File 'lib/keycard/digest_key.rb', line 35 def value if @key @key else raise HiddenKeyError, "Cannot display hashed/hidden keys" end end |