buddy

node MVC discord bot
Log | Files | Refs | README

Secretbox.js (877B)


      1 'use strict';
      2 
      3 const libs = {
      4   sodium: sodium => ({
      5     open: sodium.api.crypto_secretbox_open_easy,
      6     close: sodium.api.crypto_secretbox_easy,
      7     random: n => sodium.randombytes_buf(n),
      8   }),
      9   'libsodium-wrappers': sodium => ({
     10     open: sodium.crypto_secretbox_open_easy,
     11     close: sodium.crypto_secretbox_easy,
     12     random: n => sodium.randombytes_buf(n),
     13   }),
     14   tweetnacl: tweetnacl => ({
     15     open: tweetnacl.secretbox.open,
     16     close: tweetnacl.secretbox,
     17     random: n => tweetnacl.randomBytes(n),
     18   }),
     19 };
     20 
     21 exports.methods = {};
     22 
     23 (async () => {
     24   for (const libName of Object.keys(libs)) {
     25     try {
     26       const lib = require(libName);
     27       if (libName === 'libsodium-wrappers' && lib.ready) await lib.ready; // eslint-disable-line no-await-in-loop
     28       exports.methods = libs[libName](lib);
     29       break;
     30     } catch {} // eslint-disable-line no-empty
     31   }
     32 })();