将 sha1 摘要的一部分与 C 中的十六进制字符串进行比较

发布于 2024-10-06 16:12:47 字数 945 浏览 4 评论 0原文

我有一个字符串,我计算 sha1 摘要如下:

SHA1(sn, snLength, sha1Bin);

如果我是正确的,这会产生一个 20 字节字符(带有二进制数据)。我想将此字符的最后 3 个字节与另一个字符进行比较。该字符包含字符串“6451E6”。 64、51 和E6 是十六进制值。我如何转换“6451E6”,以便我可以通过以下方式比较它:

if(memcmp(&sha1Bin[(20 - 3)], theVarWithHexValues, 3) == 0)

{

}

我有这个函数:

/*
 * convert hexadecimal ssid string to binary
 * return 0 on error or binary length of string
 *
 */
u32 str2ssid(u8 ssid[],u8 *str) {
    u8 *p,*q = ssid;
    u32 len = strlen(str);

    if( (len % 2) || (len > MAX_SSID_OCTETS) )
      return(0);

    for(p = str;(*p = toupper(*p)) && (strchr(hexTable,*p)) != 0;) {

      if(--len % 2) {
        *q = ((u8*)strchr(hexTable,*p++) - hexTable);
        *q <<= 4;
      } else {
        *q++ |= ((u8*)strchr(hexTable,*p++) - hexTable);
      }
    }
    return( (len) ? 0 : (p - str) / 2);
}

它的作用相同,但我是 C 新手,不理解它:-(

I have a string for which I compute a sha1 digest like this:

SHA1(sn, snLength, sha1Bin);

If I'm correct this results in a 20 byte char (with binary data). I want to compare the last 3 bytes of this char with another char. This char contains the string "6451E6". 64, 51 & E6 are hex values. How do I convert "6451E6" so that I can compare it via:

if(memcmp(&sha1Bin[(20 - 3)], theVarWithHexValues, 3) == 0)

{

}

I have this function:

/*
 * convert hexadecimal ssid string to binary
 * return 0 on error or binary length of string
 *
 */
u32 str2ssid(u8 ssid[],u8 *str) {
    u8 *p,*q = ssid;
    u32 len = strlen(str);

    if( (len % 2) || (len > MAX_SSID_OCTETS) )
      return(0);

    for(p = str;(*p = toupper(*p)) && (strchr(hexTable,*p)) != 0;) {

      if(--len % 2) {
        *q = ((u8*)strchr(hexTable,*p++) - hexTable);
        *q <<= 4;
      } else {
        *q++ |= ((u8*)strchr(hexTable,*p++) - hexTable);
      }
    }
    return( (len) ? 0 : (p - str) / 2);
}

which does the same but I'm new to C and don't understand it :-(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

微凉徒眸意 2024-10-13 16:12:47

另一种方法更容易 - 将二进制数据转换为十六进制字符串进行比较:

char suffix[7];
sprintf(suffix, "%02x%02x%02x", sha1Bin[17], sha1Bin[18], sha1Bin[19]);
return stricmp(suffix, theVarWithHexValues) == 0;

即使您更喜欢转换为二进制,sscanf(...%2x...) 也比手动解析更好十六进制数字。

It's easier to go the other way — convert the binary data to a hex string for comparison:

char suffix[7];
sprintf(suffix, "%02x%02x%02x", sha1Bin[17], sha1Bin[18], sha1Bin[19]);
return stricmp(suffix, theVarWithHexValues) == 0;

Even if you prefer converting to binary, sscanf(...%2x...) is better than manually parsing hex numbers.

未蓝澄海的烟 2024-10-13 16:12:47

修复 AShelly 的代码:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int hashequal(const unsigned char *sha1Bin, const char *hexstr) {
    unsigned long hexvar = strtoul(hexstr, NULL, 16);
    unsigned char theVarWithHexValues[] = { hexvar >> 16, hexvar >> 8, hexvar };
    return memcmp(sha1Bin + 17, theVarWithHexValues, 3) == 0;
}

int main() {
    unsigned char sha1Bin[20];
    sha1Bin[17] = 0x64;
    sha1Bin[18] = 0x51;
    sha1Bin[19] = 0xE6;
    printf("%d\n", hashequal(sha1Bin, "6451E6"));
    printf("%d\n", hashequal(sha1Bin, "6451E7"));
}

Fix for AShelly's code:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int hashequal(const unsigned char *sha1Bin, const char *hexstr) {
    unsigned long hexvar = strtoul(hexstr, NULL, 16);
    unsigned char theVarWithHexValues[] = { hexvar >> 16, hexvar >> 8, hexvar };
    return memcmp(sha1Bin + 17, theVarWithHexValues, 3) == 0;
}

int main() {
    unsigned char sha1Bin[20];
    sha1Bin[17] = 0x64;
    sha1Bin[18] = 0x51;
    sha1Bin[19] = 0xE6;
    printf("%d\n", hashequal(sha1Bin, "6451E6"));
    printf("%d\n", hashequal(sha1Bin, "6451E7"));
}
埖埖迣鎅 2024-10-13 16:12:47

如果 theVarWithHexValues 确实是某种常量,那么最简单的方法就是直接将其转换为二进制形式。而不是:

const char *theVarWithHexValues = "6451E6";

使用:

const char *theVarWithHexValues = "\x64\x51\xE6";

...那么你可以直接使用 memcmp()

If theVarWithHexValues is indeed a constant of some sort, then the easiest thing would be to put it into binary form directly. Instead of:

const char *theVarWithHexValues = "6451E6";

use:

const char *theVarWithHexValues = "\x64\x51\xE6";

...then you can just memcmp() directly.

穿越时光隧道 2024-10-13 16:12:47
char* hexstr = "6451E6";
unsigned long hexvar = strtoul(hexstr, NULL, 16);
hexvar = htonl(hexvar)<<8;  //convert to big-endian and get rid of zero byte.

memcmp(&sha1Bin[(20 - 3)], (char*)hexvar, 3)
char* hexstr = "6451E6";
unsigned long hexvar = strtoul(hexstr, NULL, 16);
hexvar = htonl(hexvar)<<8;  //convert to big-endian and get rid of zero byte.

memcmp(&sha1Bin[(20 - 3)], (char*)hexvar, 3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文