Class: EdgeCastToken::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/ectoken/ectoken.rb

Class Method Summary collapse

Class Method Details

.decrypt(key, token) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/ectoken/ectoken.rb', line 19

def self.decrypt(key, token)
  digest = Digest::SHA256.digest(key)
  decoded_token = Base64.urlsafe_decode64(token)
  iv = decoded_token[0..11]
  decipher_text = decoded_token[12..decoded_token.length-17]
  decipher = OpenSSL::Cipher.new('aes-256-gcm').decrypt
  decipher.iv = iv
  decipher.key = digest
  decipher.update(decipher_text)
end

.encrypt(key, token, padding = true) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/ectoken/ectoken.rb', line 8

def self.encrypt(key, token, padding = true)
  digest = Digest::SHA256.digest(key)
  cipher = OpenSSL::Cipher.new('aes-256-gcm').encrypt
  iv = cipher.random_iv
  cipher.iv = iv
  cipher.key = digest
  cipher_text = cipher.update(token) + cipher.final
  cipher_with_iv = iv + cipher_text + cipher.auth_tag
  Base64.urlsafe_encode64(cipher_with_iv, padding: padding)
end