我在让 sha1 函数在 G-WAN 中工作时遇到了一个小问题。
基本上我有我想要散列的字符串,一般来说我是 C 的新手所以任何指针都是 太好了。
这是我正在尝试 sha1 哈希的字符串,我尝试了几种方法,但我不确定我做错了什么。
u8 *input = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
我几乎有 websockets 与 G-wan 一起工作,最后一件事是让这个 sha1 函数与我合作。
下面是G-wans sha1函数的文档
// u8 dst[20]; // the resulting 160-bit hash
// sha1_t ctx;
// sha1_init(&ctx);
// int i = 10;
// while(i--)
// sha1_add(&ctx, data[i].ptr, data[i].len);
// sha1_end(&ctx, dst);
typedef struct { u8 x[220]; } sha1_t;
void sha1_init(sha1_t *ctx);
void sha1_add (sha1_t *ctx, u8 *src, int srclen);
void sha1_end (sha1_t *ctx, u8 *dst);
// a wrapper on all the above SHA-160 calls
void sha1(u8 *input, int ilen, u8 *dst);
API 链接 http://gwan.com/api
如果有人能给我一个机会,这会让我在 C 的最后几个小时变得有点宽容。
最佳答案
这是一个关于如何使用 sha1 函数的示例。
u8 input[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
u8 result[20];
sha1(input, sizeof(input) - 1, result);
xbuf_xcat(get_reply(argv), "SHA1 Result: %20B", result);
结果是二进制的,因此您需要将其转换为 B64 或 HEX 以使其可读。 “%B”是 G-WAN 中的 B64 转换。 '%20B' 告诉它转换前 20 个字节。
结果:
SHA1 Result: Kfh9QIsMVZcl6xEPYxPHzW8SZ8w=
关于c - G-wan C Sha1 散列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13254000/