Class: D2L::Valence::Encrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/d2l/valence/encrypt.rb

Overview

Encrypt

Class encrypt and encode data for transmission in a URL

Class Method Summary collapse

Class Method Details

.encode(digest) ⇒ Object



18
19
20
21
22
# File 'lib/d2l/valence/encrypt.rb', line 18

def self.encode(digest)
  Base64.urlsafe_encode64(digest, padding: false).strip
rescue
  old_encode digest
end

.generate_from(key, data) ⇒ Object

Encrypt and encode data with provided key

Parameters:

  • key (String)

    the key to encrypt with

  • data (String)

    data to encrypt and encode



10
11
12
# File 'lib/d2l/valence/encrypt.rb', line 10

def self.generate_from(key, data)
  encode(sign(key, data))
end

.old_encode(digest) ⇒ Object

support for older versions of ruby



25
26
27
# File 'lib/d2l/valence/encrypt.rb', line 25

def self.old_encode(digest)
  remove_unwanted_chars Base64.encode64(digest).strip
end

.remove_unwanted_chars(string) ⇒ Object



29
30
31
# File 'lib/d2l/valence/encrypt.rb', line 29

def self.remove_unwanted_chars(string)
  string.delete('=').tr('+', '-').tr('/', '_').strip
end

.sign(key, data) ⇒ Object



14
15
16
# File 'lib/d2l/valence/encrypt.rb', line 14

def self.sign(key, data)
  OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), key, data)
end