如何访问数以百万计的位进行哈希
How to access millions of bits for hashing
我正在对可执行文件进行MD5哈希。我已经使用python脚本从可执行文件读取二进制文件到文本文件,但是如果我要将这个构造文件读取到C程序中,我将处理mb的数据,因为1和0被视为char
s,每个1位数字取8位。是否有可能将它们分别读取为单个比特?如果我制作了一个10MB的数组来保存二进制转换长度和哈希填充所需的所有字符,那么程序的性能会有多差?如果这是不可想象的,有没有更好的方法来操纵数据?
既然你标注了C和c++,我选C。
有可能将这些读取为单个比特吗?
是的,每次从文件中读取8个字节,并将这些1
s和0
s连接成一个新字节。您不需要为此创建一个10MB的数组。
首先,从文件中读取8个字节。读取的char
值将被转换为整数值(0
和1
),然后进行位移以生成一个新字节。
unsigned char bits[8];
while (fread(bits, 1, 8, file) == 8) {
for (unsigned int i = 0; i < 8; i++) {
bits[i] -= '0';
}
char byte = (bits[0] << 7) | (bits[1] << 6) |
(bits[2] << 5) | (bits[3] << 4) |
(bits[4] << 3) | (bits[5] << 2) |
(bits[6] << 1) | (bits[7] );
/* update MD5 Hash here */
}
然后,用新读取的字节更新MD5哈希值。
Edit:由于典型的MD5实现必须在处理之前将输入分解为512位的块,因此您可以在实现本身中消除该开销(尽管不推荐),只需从文件中读取512位(64字节),然后直接更新哈希。
unsigned char buffer[64];
unsigned char bits[8];
unsigned int index = 0;
while (fread(bits, 1, 8, file) == 8) {
for (unsigned int i = 0; i < 8; i++) {
bits[i] -= '0';
}
buffer[index++] = (bits[0] << 7) | (bits[1] << 6) |
(bits[2] << 5) | (bits[3] << 4) |
(bits[4] << 3) | (bits[5] << 2) |
(bits[6] << 1) | (bits[7] );
if (index == 64) {
index = 0;
/* update MD5 hash with 64 byte buffer */
}
}
/* This sends the remaining data to the MD5 hash function */
/* It's not likely that your file has exactly 512N chars */
if (index != 0) {
while (index != 64) {
buffer[index++] = 0;
}
/* update MD5 hash with the padded buffer. */
}
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 使用Qt C++计算类似Git的SHA1哈希
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 用C++将哈希表写入文件并从文件中恢复
- c++找不到具有相同哈希的无序集合元素
- 哈希文件递归并保存到矢量Cryptopp中
- 对 pair<pair<int,int>pair<int,int unordered_map进行哈希处理>>
- 直接在 unordered_map 的方法中使用哈希,而不是生成哈希的用户定义对象
- 如何为字符串生成唯一但一致的 N 位哈希(小于 64 位)?
- 使用对象的基类部分模板专用化对对象进行哈希处理::哈希
- boost::包含提升单元的元组的哈希值
- 使用 Key 对 C++ 中的哈希映射进行排序. 无法排序
- C++中的并发哈希表
- 哈希映射使用 nullptr c++ 初始化节点的动态数组
- 在具有开放寻址的哈希表中插入节点 [优化逻辑]
- 与C++哈希表的基础知识混淆
- 如何为位集找到/实现一个好的哈希函数
- 对于短字符串来说,这是一个很好的哈希函数吗?
- 如何访问数以百万计的位进行哈希