class EdgeCastToken::Token

Public Class Methods

decrypt(key, token) click to toggle source
# 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) click to toggle source
# 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