Class: RCL::Token

Inherits:
Object show all
Defined in:
lib/rcl/token.rb

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Token

Returns a new instance of Token.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rcl/token.rb', line 10

def initialize(key)
  raise ArgumentError, 'nil key' if key.nil?
  raise ArgumentError, 'invalid key class' if key.class != String
  raise ArgumentError, 'empty key' if key.empty?

  srand

  @salt = [Array.new(6) { rand(256).chr }.join].pack("m").chomp
  @hash = Digest::SHA256.hexdigest(@salt+key)

  nil
end

Instance Method Details

#authenticate(key) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
# File 'lib/rcl/token.rb', line 23

def authenticate(key)
  raise ArgumentError, 'nil key' if key.nil?
  raise ArgumentError, 'invalid key class' if key.class != String
  raise ArgumentError, 'empty key' if key.empty?

  Digest::SHA256.hexdigest(@salt+key) == @hash
end