- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在 C++(准确地说是 C++11)中,我想获取包含 NUL 字符(所有八位都设置为 0 的 ASCII 字符)的字符串的 HMAC SHA512 哈希值。
使用 crypto++,到目前为止我的方法如下:
std::string message("My\0Message",2+1+7);
std::string key_base64 = "MYSECRETKEY";
std::string key;
StringSource ss(key_base64, true, new Base64Decoder(new StringSink(key)));
std::string mac, encoded;
HMAC< SHA512 > hmac((byte*)key.c_str(), key.length());
StringSource(message,
true,
new HashFilter(hmac, new StringSink(mac))
); // StringSource
encoded.clear();
StringSource(mac,
true,
new Base64Encoder(new StringSink(encoded), false) // Base64Encoder. Pass argument 'false' to prevent insertion of line breaks
); // StringSource
std::cout << "Hashed message (Base64): " << encoded << std::endl;
当上面的 message
字符串中包含 NUL 字符时,这将无法正常工作。
我得到的散列(变量 mac
)的 base64 编码版本是
bXmQCkzhzq3J81vInF6IRtxXyd+mN8TRh8e3qHThJ+/RYVcgRkFZ9iStKnNaVsGgco/nisUeRpT3m388UR/BMg==
而不是预期的
hUk4PX3mnqs943JnMR+ptW6f8+enIUGBd4x7sUA+Ug2squOFVF6ZdiCewSBDlGAwNuWf+9Uh0AqUkQV1vMNHxg==
编辑
可以从 Bash 命令行获得预期的输出,如下所示:
hex_encoded_secret=$(echo -n "MYSECRETKEY" | base64 --decode | xxd -p | tr '\n' ' ' | tr -d '[:space:]')
echo -ne "My\0Message" | openssl dgst -sha512 -mac HMAC -macopt hexkey:"${hex_encoded_secret}" -binary | base64 | tr -d '\n'
这会生成上面给出的预期输出。
最佳答案
This doesn't work properly when a NUL character is included as in the message string above.
您使用的构造函数应该没问题,因为机器使用 std::string::size()
,而不是 std::string::c_str()
来确定长度。我怀疑还有其他问题不太正确,例如字符编码问题。
您正在使用 StringSource
的 std::string
构造函数。 StringSource
的第二个构造函数采用二进制字符串。在您的生产代码中使用它可能会更好:
StringSource(reinterpret_cast<const byte*>(&message[0]),
message.size(),
true,
new HashFilter(hmac, new StringSink(mac))
); // StringSource
另见 StringSource
在 Crypto++ wiki 上。
关于c++ - 使用 crypto++ 在 C++ 中散列包含 NUL 字符的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49477520/
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 7 年前。 Improve t
我有一个 const char* 指向十六进制格式的数据,我需要找到我正在检查的数据的长度 NUL-terminator 但是当\x00 出现时它检测到它作为 NUL 终止符返回不正确的长度。 我该如
如果 nul 永远不能成为 Windows 中的一个文件,那么如果使用对 nul 没有相同规则的操作系统在所有目录中创建一个名为“nul”的文件会怎么样。例如,如果在 CMD 中执行“echo 123
我知道 strlen() 不计算 NUL 终止字符。我真的知道这是事实。因此,这个问题并不是要问为什么 strlen() 可能“大概”不返回正确的字符串长度,这个问题已经在 StackOverfl
虽然 CStr 通常用于 FFI,但我正在读取 &[u8],它以 NUL 结尾并确保是有效的 UTF-8,所以没有需要检查。 但是 NUL 终止符不一定在切片的末尾。将此作为 &str 获取的好方法是
虽然 CStr 通常用于 FFI,但我正在读取 &[u8],它以 NUL 结尾并确保是有效的 UTF-8,所以没有需要检查。 但是 NUL 终止符不一定在切片的末尾。将此作为 &str 获取的好方法是
我正在使用 Java BufferedWriter 逐行写入文件。问题是,当我写完文件时,它在整个文件中显示“nul”。 我不认为问题出在我正在写的内容上,因为我用System.out.print仔细
我正在使用 R 使用 fread 读取一个文件,该文件包含许多列和行。该文件如下所示: 1_17118 1_18353 1_21882 1_21955 1_22054 Ind0001 -1
c89 海合会 (海湾合作委员会) 4.6.3 你好, 我在 *search = '\0' 上得到一个堆栈转储;我认为可以用 nul 终止一个字符串。 char *ptr_name = "hello@
这个问题在这里已经有了答案: NUL undeclared- first use in this function (4 个答案) 关闭 8 年前。 char word[5]={"fayed"};
我曾经使用以下函数写入很多不同的文件 using (FileStream fs = new FileStream(Settings.PsLog, FileMode.Truncate, System.S
我正在编写一个下载器,它将 url 拆分为多个部分并使用线程下载,可能我不会使用“join”,因为 join = 无法流式传输(如果所有线程未完成,则无法写入文件) 但是问题是 f.seek 和 wr
这是我服务器端的方法: void send_err(int socket_fd, char *msg) { /// To send an error message (`ERR`).
我需要创建一个将标准错误流重定向到某个文件的新进程。创建子进程的代码没有可用的控制台,因此在某些情况下 GetStdHandle(any) 将返回 0。子进程将出于某种原因尝试复制其所有标准 IO 句
我的 Notepad++ 中有这样的字符 当我尝试复制整行时,我实际上正在复制所有内容,直到“NUL”: File:1 我想做的就是替换那些空的,什么都没有,这样我就可以复制我的整行。也许有任何关键字
我正在通过 Java 中的 UDP 接收带有字节数组的数据包。我知道最大可能的字节大小,但我不知道当前接收的大小。 如果我从这个字节数组创建一个 String 实例,那么该字符串在有用的有效负载之后将
来自 the web我知道 C 包含 NUL 关键字。 编译时出现错误 NUL undeclared first use in this function' 我的代码片段: for (;;) {
#include #include int main() { char x[] = "Happy birthday to You"; char y[25]; char z[
我看到了here如果它不是 NULL 终止的,则不可能使用 strlen 找出 (unsigned char *) 字符串长度,因为 strlen 函数将遍历字符串但不会找到任何 '\0',因此运行-
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我是一名优秀的程序员,十分优秀!