Class: Auther::Cipher

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

Overview

Manages encryption/decryption.

Constant Summary collapse

BYTE_DIVISOR =
2

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret) ⇒ Cipher

Returns a new instance of Cipher.



19
20
21
# File 'lib/auther/cipher.rb', line 19

def initialize secret
  @encryptor = ActiveSupport::MessageEncryptor.new secret
end

Class Method Details

.generate(login, password) ⇒ Object



8
9
10
11
12
13
# File 'lib/auther/cipher.rb', line 8

def self.generate , password
  secret = SecureRandom.hex key_length / BYTE_DIVISOR
  cipher = new secret

  {secret:, login: cipher.encrypt(), password: cipher.encrypt(password)}
end

.key_lengthObject



15
16
17
# File 'lib/auther/cipher.rb', line 15

def self.key_length
  ActiveSupport::MessageEncryptor.key_len
end

Instance Method Details

#decrypt(data) ⇒ Object



27
28
29
# File 'lib/auther/cipher.rb', line 27

def decrypt data
  encryptor.decrypt_and_verify data
end

#encrypt(data) ⇒ Object



23
24
25
# File 'lib/auther/cipher.rb', line 23

def encrypt data
  encryptor.encrypt_and_sign data
end