将代码从 JS 转换为 CPP
Translate code from JS to CPP
我对 CPP 有点经验,对 JS 完全陌生。有一项任务是将代码从 JS 转换为 CPP,我尝试了一些但不清楚我在做什么;以下是我的js代码。
body: json object
xyz: string (suck as: ALgalgw7agw)
const payload = new Buffer(JSON.stringify(body))
.toString('base64')
const signature = crypto
.createHmac('sha384', xyz)
.update(payload)
.digest('hex')
1)这段代码是什么意思?
2) 如何在 cpp 中实现它?
非常感谢您的宝贵时间,
你说的是 NodeJS,在这种情况下,Buffer class 的实例类似于整数数组,但对应于 fixed-sized 原始内存分配。 Buffer的大小在创建时就确定了,不能调整大小。之后,您将其转换为 Base64 字符串。
const buf = Buffer.from('hello world', 'ascii');
// Prints: aGVsbG8gd29ybGQ=
console.log(buf.toString('base64'));
之后,您将为有效负载创建哈希,使用 "xyz" 变量作为解密有效负载的密钥,然后将其消化为十六进制。所以最后:
signature = /* hex sha384-with-key encrypted payload */
如果您想将此 JS 代码转换为 C++,您需要使用某种加密库(例如 OpenSSL),对于 Buffer,我认为有 built-in 函数。
我建议您阅读 this and this 以了解有关 NodeJS 上的加密和缓冲区 classes/functions 的更多信息
我对 CPP 有点经验,对 JS 完全陌生。有一项任务是将代码从 JS 转换为 CPP,我尝试了一些但不清楚我在做什么;以下是我的js代码。
body: json object
xyz: string (suck as: ALgalgw7agw)
const payload = new Buffer(JSON.stringify(body))
.toString('base64')
const signature = crypto
.createHmac('sha384', xyz)
.update(payload)
.digest('hex')
1)这段代码是什么意思?
2) 如何在 cpp 中实现它?
非常感谢您的宝贵时间,
你说的是 NodeJS,在这种情况下,Buffer class 的实例类似于整数数组,但对应于 fixed-sized 原始内存分配。 Buffer的大小在创建时就确定了,不能调整大小。之后,您将其转换为 Base64 字符串。
const buf = Buffer.from('hello world', 'ascii');
// Prints: aGVsbG8gd29ybGQ=
console.log(buf.toString('base64'));
之后,您将为有效负载创建哈希,使用 "xyz" 变量作为解密有效负载的密钥,然后将其消化为十六进制。所以最后:
signature = /* hex sha384-with-key encrypted payload */
如果您想将此 JS 代码转换为 C++,您需要使用某种加密库(例如 OpenSSL),对于 Buffer,我认为有 built-in 函数。
我建议您阅读 this and this 以了解有关 NodeJS 上的加密和缓冲区 classes/functions 的更多信息