npm base32 不工作。是我的代码吗?
npm base32 not working. Is it my code?
我在使用 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" 和 "decoded" 不同。他们不应该是一样的吗?哪里错了?
您必须解码编码值。
decoded = base32.decode(encoded);
我在使用 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" 和 "decoded" 不同。他们不应该是一样的吗?哪里错了?
您必须解码编码值。
decoded = base32.decode(encoded);