Skip to content

Instantly share code, notes, and snippets.

@rcyrus
Forked from kakoni/example.rb
Created May 31, 2017 16:04
Show Gist options
  • Save rcyrus/ef0cc9e25eca3167cac4dc0db5170908 to your computer and use it in GitHub Desktop.
Save rcyrus/ef0cc9e25eca3167cac4dc0db5170908 to your computer and use it in GitHub Desktop.
Blind indexes using ruby
require 'rbnacl'
require 'base64'
class Example
attr_reader :secret_box, :index_key
def initialize
key = RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
@index_key = RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
@secret_box = RbNaCl::SecretBox.new(key)
end
def encrypt_ssn(ssn)
nonce = RbNaCl::Random.random_bytes(secret_box.nonce_bytes)
ciphertext = secret_box.encrypt(nonce, ssn)
nonce.concat(ciphertext)
end
def decrypt_ssn(ciphertext)
nonce = ciphertext[0..secret_box.nonce_bytes - 1]
secret_box.decrypt(nonce, ciphertext[secret_box.nonce_bytes..-1])
end
def get_ssn_blind_index(ssn)
salt = RbNaCl::Hash.blake2b(index_key, digest_size: 16)
# Argon2 CPU and memory cost parameters
opslimit = 5
memlimit = 7_256_678
# Size of digest to compute in bytes (default 64)
digest_size = 64
digest = RbNaCl::PasswordHash.argon2(
ssn,
salt,
opslimit,
memlimit,
digest_size
)
Base64.encode64(digest)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment