Sha256: 408209d47b68a074a42b3deed620ab95b4bf38a745a5371f9670983c253a9b66
Contents?: true
Size: 1.09 KB
Versions: 22
Compression:
Stored size: 1.09 KB
Contents
# frozen_string_literal: true require 'singleton' require 'rbnacl' require 'base64' module NanoBot module Components class Crypto include Singleton def initialize password = ENV.fetch('NANO_BOTS_ENCRYPTION_PASSWORD', nil) password = 'UNSAFE' unless password && password != '' @box = RbNaCl::SecretBox.new(RbNaCl::Hash.sha256(password)) @fixed_nonce = RbNaCl::Hash.sha256(password)[0...@box.nonce_bytes] end def encrypt(content, soft: false) nonce = soft ? @fixed_nonce : RbNaCl::Random.random_bytes(@box.nonce_bytes) Base64.urlsafe_encode64(nonce + @box.encrypt(nonce, content)) end def decrypt(content) decoded_content = Base64.urlsafe_decode64(content) nonce = decoded_content[0...@box.nonce_bytes] cipher_text = decoded_content[@box.nonce_bytes..] @box.decrypt(nonce, cipher_text) end def self.encrypt(content, soft: false) instance.encrypt(content, soft:) end def self.decrypt(content) instance.decrypt(content) end end end end
Version data entries
22 entries across 22 versions & 1 rubygems
Version | Path |
---|---|
nano-bots-0.1.0 | components/crypto.rb |
nano-bots-0.0.10 | components/crypto.rb |