npm base32无法正常工作。这是我的代码吗?

时间:2017-01-11 09:15:43

标签: javascript node.js base32

我遇到了base32 npm包的问题。我构建了一个最小的脚本来测试一般的功能,但我仍然会犯错误。我在这里盲目地遗漏了一些东西还是npm包破了?

'use strict';
        
const base32 = require('base32');
const crypto = require('crypto');
        
let val = "";
let encoded = "";
let decoded = "";

for(let i = 0; i < 3; i++) {
  //Generate a random string
  val = crypto.randomBytes(64).toString('hex'); //or base64 instead of hex
  //endode it in base32
  encoded = base32.encode(val);
  //decode it again.
  decoded = base32.decode(val);
          
  //val and decoded should be equal now
  if(decoded !== val)  {
    console.log('FATAL ERROR ' + i);
    console.log('val: ' + val);
    console.log('enc: ' + encoded);
    //The console output of decoded looks like binary rubbish
    console.log('dec: ' + decoded);
  }     
}

现在所有随机值都导致“val”和“decode”不同。它们不应该是一样的吗?错误在哪里?

1 个答案:

答案 0 :(得分:0)

您必须解码编码值。

 decoded = base32.decode(encoded);