-
-
Save alvinkatojr/abad0cfc73ee4128076e14f6504b7504 to your computer and use it in GitHub Desktop.
Blind indexes using ruby (as per https://paragonie.com/blog/2017/05/building-searchable-encrypted-databases-with-php-and-sql)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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).unpack('H*').first | |
| end | |
| def decrypt_ssn(ciphertext) | |
| ciphertext = [ciphertext].pack('H*') | |
| 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) | |
| digest = RbNaCl::PasswordHash.argon2( | |
| ssn, | |
| salt, | |
| RbNaCl::PasswordHash::Argon2::OPSLIMIT_INTERACTIVE, | |
| RbNaCl::PasswordHash::Argon2::MEMLIMIT_INTERACTIVE, | |
| 33 | |
| ) | |
| digest.unpack('H*').first | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment