简体   繁体   English

与加密的 randomBytes 异或

[英]XOR with crypto's randomBytes

I want to create a master key where I XOR 3 random keys (generated with crypto.randomBytes ).我想创建一个主密钥,其中我 XOR 3 个随机密钥(使用crypto.randomBytes生成)。

A,B,C = crypto.randomBytes(32)
MASTER_KEY = A ^ B ^ C;

I am not sure how to make this work in Javascript.我不确定如何在 Javascript 中完成这项工作。 randomBytes returns a Buffer. randomBytes返回一个缓冲区。 I'm not sure if I have to do a .toString() on it or just perform the XOR as a Buffer?我不确定是否必须对其执行.toString()或仅将 XOR 作为缓冲区执行?

This should do:这应该这样做:

const BUF_LEN = 32
const result = Buffer.alloc(BUF_LEN)

for (let i = 0; i < BUF_LEN; i++) {
  const [a, b, c] = [A.readUInt8(i), B.readUInt8(i), C.readUInt8(i)]
  result.writeUInt8(a ^ b ^ c, i)
}

console.log(result.toString('hex'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM