Class: Ubiq::Algo
- Inherits:
-
Object
- Object
- Ubiq::Algo
- Defined in:
- lib/ubiq/algo.rb
Overview
Class to provide some basic information mapping between an encryption algorithm name and the cooresponding key size, initialization vector length, and tag
Constant Summary collapse
- UBIQ_HEADER_V0_FLAG_AAD =
0b00000001
Instance Method Summary collapse
- #decryptor(obj, key, iv) ⇒ Object
- #encryptor(obj, key, iv = nil) ⇒ Object
- #find_alg(id) ⇒ Object
- #get_algo(name) ⇒ Object
- #set_algo ⇒ Object
Instance Method Details
#decryptor(obj, key, iv) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ubiq/algo.rb', line 66 def decryptor(obj, key, iv) raise RuntimeError, 'Invalid key length' if key.length != obj[:key_length] raise RuntimeError, 'Invalid initialization vector length' if !iv.nil? && iv.length != obj[:iv_length] cipher = obj[:mode] cipher.decrypt cipher.key = key cipher.iv = iv return cipher end |
#encryptor(obj, key, iv = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ubiq/algo.rb', line 49 def encryptor(obj, key, iv = nil) # key : A byte string containing the key to be used with this encryption # If the caller specifies the initialization vector, it must be # the correct length and, if so, will be used. If it is not # specified, the function will generate a new one raise RuntimeError, 'Invalid key length' if key.length != obj[:key_length] raise RuntimeError, 'Invalid initialization vector length' if !iv.nil? && iv.length != obj[:iv_length] cipher = obj[:mode] cipher.encrypt cipher.key = key iv = cipher.random_iv return cipher, iv end |
#find_alg(id) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/ubiq/algo.rb', line 36 def find_alg(id) set_algo.each do |k,v| if v[:id] == id return k end end "unknown" end |
#get_algo(name) ⇒ Object
45 46 47 |
# File 'lib/ubiq/algo.rb', line 45 def get_algo(name) set_algo[name] end |
#set_algo ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ubiq/algo.rb', line 15 def set_algo @algorithm = { 'aes-256-gcm' => { id: 0, algorithm: OpenSSL::Cipher::AES256, mode: OpenSSL::Cipher::AES256.new(:GCM), key_length: 32, iv_length: 12, tag_length: 16 }, 'aes-128-gcm' => { id: 1, algorithm: OpenSSL::Cipher::AES128, mode: OpenSSL::Cipher::AES128.new(:GCM), key_length: 16, iv_length: 12, tag_length: 16 } } end |