https://github.com/paragonie/ciphersweet-js/tree/master/docs
const sodium = require('sodium-native');
let keyMaterial = Buffer.alloc(32, 0);
sodium.randombytes_buf(keyMaterial);
console.log(keyMaterial.toString('hex'));
e817a2cff380f0eda1650d45341832cca04c10203dd160b4ab4df250ff6e5f37
const {
FIPSCrypto,
BlindIndex,
CipherSweet,
CompoundIndex,
EncryptedRow,
LastFourDigits,
StringProvider
} = require('ciphersweet-js');
let provider = new StringProvider(
// Example key, chosen randomly, hex-encoded:
'e817a2cff380f0eda1650d45341832cca04c10203dd160b4ab4df250ff6e5f37'
);
let engine = new CipherSweet(provider, new FIPSCrypto());
// Using the EncryptedRow abstraction:
let contactEncrypter = new EncryptedRow(engine, 'contacts')
.addTextField('first_name')
.addTextField('last_name')
.addBooleanField('hiv_status')
.addTextField('insurance_id')
.addTextField('ssn')
.addCompoundIndex(
new CompoundIndex(
'ssn_insurance_id_last4',
['insurance_id', 'ssn'],
16
)
)
.addBlindIndex('insurance_id', new BlindIndex('insurance_id_idx', [], 8))
.addBlindIndex('ssn', new BlindIndex('ssn_last4_idx', [new LastFourDigits()], 8));
// An example row that we might want to store, encrypting some fields in the process...
let exampleRow = {
"id": 12345,
"first_name": "Harvey",
"last_name": "Dent",
"hiv_status": false,
"insurance_id": "A1234-567-89012",
"ssn": "123-45-6789"
};
// if wrapped in an async function, use await instead:
(async function() {
[encryptedRow, indexes] = await contactEncrypter.prepareRowForStorage(exampleRow);
console.log(encryptedRow, indexes);
})();
{
id: 12345,
first_name: 'fips:Csq6a-LeJ3Z2w-r3u-cDAc1za6LaLVKzh0feXxHDMJZGKW7k8Dfmun6AAoOmvEoxlO8i_m816-c28afX-xPkkPn1UTmUBz7JDZpZRqfsYppbLq89ikWuAijmmud7CAdPftSSjgks',
last_name: 'fips:pr1g--CjQCKDuDMc95R6uNNHwexwY6LwBiaFOohfkPHaN9y1w7O4Au4afbPPKICea7Ae4SDspFgP_w9abhfFnMYIy4pEcctWjtccft8xotQYKHKxOAXPr2QVqAlZFvHioM5ASg==',
hiv_status: 'fips:iqcB9nrVNQhu5ExWUfHQjEKzBz8IYqD4Qw2RiZ-TKHtgAhu66K0jwpMovZkycxugRiZMk-zDFkyi4HB5lZzOCrSIax5sBGbK1P0RzK5fNf8Nt38hvY9F1zNIFBxwYcJqcA==',
insurance_id: 'fips:bRRCn9mRL3LJZo6aopAdJVh5zeHiaTy4grM56Oq-_teMxAHhpfRA91XwQai8Yzi45n6x5acUUKCE95f3ey5pWpgJ0KHD4I9H87eYd43j1KmIggRrsOMo6IA2Q__n9pG01GA9nLwKth32Z54WJwKE',
ssn: 'fips:zkVA9zZsGtAlL0hGP7ArKvYOAhY6K_grlnf4E7ox2Apea-6XF1UEtLR38ialuFWFczkWI6KZcv3gGqTEqgP9JQu0U_khzY4IrByb-AgQY-Ghvd_tlqBjRWTnT3toeQw-3eBpfSuXK6IeJro='
} {
insurance_id_idx: '88',
ssn_last4_idx: 'f1',
ssn_insurance_id_last4: '8145'
}
{
id: 12345,
first_name: 'fips:mpx6ccVNU39xpKIOyRLZ1UownxcH8DVrHN-Ierzn8TzZbV-5S-qd2TUc-eWSVmYSegu5C20x7VRBXz1XeXqwZskMpnu9sNxtXLV6p17yepvi5aPnzrB2__8ClGXwKYfTsJHyzS-q',
last_name: 'fips:boEcrxnk0WqTqRlv-5XrtHuDOVu0FhjPkEt92XRY3ZhZjz6_QSkkq9KmELH5d5EE1D7RKfrzNqtRXVZOaaoMYSGcy6wX0j-jAaeBXs-wZUBz2-x0tugF0K4-R_uC-OhbMXbZtg==',
hiv_status: 'fips:vEwawO0cexUWknqqACi109sdzWdr3oVEjtZp_SDDqjR_UVZ_k3WezEVBNRDLKJIFwx0Ci0GfSSzyK4Wjk0Dntw8Mt4wHKtsh3xbCWEHsoUldIkP2VexwqjTQa30U3EeutA==',
insurance_id: 'fips:VRDOvVIjo5Agq5Zrv_ZcdXh97WvgY6XVd-pv8bZFUenlMm3kKJXpIAaW-CZ3YyWQ7l-aMkdvMYdKIiWqP9Lad0wunpjgQ2LYr8znHW4WXmYgKyYnFIwZRAXym66WLmfH5GHc3EaRwlv9xjsqRV2k',
ssn: 'fips:fZXbCnOTTs65DniRdtyRygnn0EsMG6M2ZFwEuaEHgwea4Ax0hf-82ZyOS-Un-V68-BdqWm_Rn65nr4tOR4OXEVB7zRpI81YewLoVJUp0WPN96_Zi39TfJ02PSUPAXoLE5c9OK7XEjrcKEMA='
} {
insurance_id_idx: '88',
ssn_last4_idx: 'f1',
ssn_insurance_id_last4: '8145'
}