не может реплицировать код в пакете node-fpe - PullRequest
0 голосов
/ 06 мая 2020

Я новичок ie с JS и работаю над реализацией шифрования, нашел этот полезный пакет в github (https://github.com/mderazon/node-fpe). Однако мне сложно воспроизвести код, упомянутый в git README.

Для установки пакета я сделал следующее:

git clone https://github.com/mderazon/node-fpe.git
npm install node-fpe

Затем в том же каталоге я использую node для локального запуска команд, упомянутых на странице git, вот ошибка, которую я получаю:

> const fpe = require('node-fpe');
/home/pooja/encryption-task/node_modules/node-fpe/lib/index.js:4
module.exports = function({
                          ^
SyntaxError: Unexpected token {
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:374:25)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at repl:1:13
    at REPLServer.defaultEval (repl.js:252:27)
    at bound (domain.js:287:14)
> 

В коде фрагмент кода записан следующим образом:

const crypto = require('crypto');                                                                                                                                        
const digits = '1234567890'.split('');                                                                                                                                   

module.exports = function({                                                                                                                                              
  password,                                                                                                                                                              
  algorithm = 'aes-256-cbc',                                                                                                                                             
  domain = digits                                                                                                                                                        
}) {                                                                                                                                                                     
  if (!password) {                                                                                                                                                       
    throw new Error('`password` is required');                                                                                                                           
  }                                                                                                                                                                      

  function enc(text) {                                                                                                                                                   
    const cipher = crypto.createCipher(algorithm, password);                                                                                                             
    let crypted = cipher.update(text, 'utf8', 'hex');                                                                                                                    
    crypted += cipher.final('hex');                                                                                                                                      
    return crypted;                                                                                                                                                      
  }                                                                                                                                                                      

  // create a permutation of domain                                                                                                                                      
  const sorted = domain                                                                                                                                                  
    .map(c => c)                                                                                                                                                         
    .sort((c1, c2) => enc(c1).localeCompare(enc(c2)));                                                                                                                   
  const encTable = {};                                                                                                                                                   
  const decTable = {};                                                                                                                                                   

  for (let i in domain) {                                                                                                                                                
    encTable[domain[i]] = sorted[i];                                                                                                                                     
    decTable[sorted[i]] = domain[i];                                                                                                                                     
  }                                                                                                                                                                      

  function validate(text, result) {                                                                                                                                      
    if (text.length !== result.length) {                                                                                                                                 
      throw new Error(                                                                                                                                                   
        `some of the input characters are not in the cipher's domain: [${domain}]`                                                                                       
      );                                                                                                                                                                 
    }                                                                                                                                                                    
  }                                                                                                                                                                      

Кстати, я ничего не менял в исходном коде node-fpe. Мы высоко ценим любое понимание проблемы, большое спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...