forked from ISS-Security/hw-credit_card_crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsk_cipher.rb
26 lines (23 loc) · 866 Bytes
/
sk_cipher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
require 'rbnacl/libsodium'
require 'base64'
# Modern Symmetric Cipher
module ModernSymmetricCipher
def self.generate_new_key
# TODO: Return a new key as a Base64 string
key = RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
Base64.encode64(key)
end
def self.encrypt(document, key)
# TODO: Return an encrypted string
# Use base64 for ciphertext so that it is sendable as text
simple_box = RbNaCl::SimpleBox.from_secret_key(Base64.decode64(key))
simple_ciphertext = simple_box.encrypt(document)
Base64.encode64(simple_ciphertext)
end
def self.decrypt(aes_crypt, key)
# TODO: Decrypt from encrypted message above
# Expect Base64 encrypted message and Base64 key
simple_box = RbNaCl::SimpleBox.from_secret_key(Base64.decode64(key))
simple_box.decrypt(Base64.decode64(aes_crypt))
end
end