获取 UTF-16le 字符串的 SHA256 哈希值

Get SHA256 hash of UTF-16le String

我正在尝试获取 UTF-16le 字符串的 SHA256 和。 Python从理论上讲,我正在尝试做的事情看起来像这样:

import hashlib
username = "Administrator"
username = username.decode('utf-8').encode('utf-16le')
hash = hashlib.sha256(username).digest()
print(hash)

下面的 C 代码为我提供了 SHA256 哈希,就好像我没有在上面的 Python 部分中调用 decode('utf-8').encode('utf-16le')

下面的输出是e7d3e769f3f593dadcb8634cc5b09fc90dd3a61c4a06a79cb0923662fe6fae6b。我想要的输出是 5264c63204c56c0df9f8f4a030ea19d93a0fa402be6b00b4d7464e61641021f7

这是我第一次用 C 编写代码,所以如果我遗漏了明显的东西或做错了什么,这就是原因。

#include <openssl/sha.h>
#include <stdio.h>
#include <string.h>

int main()
{
    unsigned const char ibuf[] = "Administrator";
    unsigned char obuf[32];

    SHA256(ibuf, strlen((const char * )ibuf), obuf);

    unsigned char hash[32];
    int i;
    for(i = 0; i < 32; i++)
    {
        printf("%02x",obuf[i]);
    }

    printf("\n");
    return 0;
}

查看您的代码,唯一剩下要做的就是字符集转换。进行该转换的一种方法是使用 iconv 函数族。

调整 this question 并将其与您的代码结合起来,可能类似于:

#include <stdio.h>
#include <string.h>
#include <iconv.h>
#include <openssl/sha.h>

int main()
{
    unsigned char ibuf[] = "Administrator";
    unsigned char obuf[32];

    char dest_str[100];
    char *in = ibuf;
    char *out = dest_str;
    size_t inbytes = strlen(in);
    size_t outbytes = sizeof dest_str;
    iconv_t conv = iconv_open("UTF-16LE", "UTF-8");

    if (conv == (iconv_t)-1) {
        perror("iconv_open");
        return 1;
    }

    if (iconv(conv, &in, &inbytes, &out, &outbytes) == (size_t)-1) {
        perror("iconv");
        return 1;
    }

    iconv_close(conv);

    SHA256(dest_str, sizeof dest_str - outbytes, obuf);

    unsigned char hash[32];
    int i;
    for(i = 0; i < 32; i++)
    {
        printf("%02x",obuf[i]);
    }

    printf("\n");
    return 0;
}

...这确实应该给出:

5264c63204c56c0df9f8f4a030ea19d93a0fa402be6b00b4d7464e61641021f7

注意几点:

  • iconv_open 函数首先使用“to”字符集,然后使用“from”字符集。
  • iconv 函数 改变了 它传递的东西:inoutinbytesoutbytes 都被修改(增加或减少)为 iconv 转换字符。
  • iconv 不为结果分配内存 - 在示例程序中,它使用固定大小的 100 字节缓冲区来存储其输出。如果您还要转换更长的字符串,您可能需要更大的缓冲区。