SHA算法c语言
时间: 2023-07-26 09:12:20 浏览: 43
SHA算法在C语言中的实现同样可以使用openssl库。以下是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
int main()
{
char str[] = "hello world";
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256((const unsigned char*) str, strlen(str), hash);
char sha256string[SHA256_DIGEST_LENGTH*2+1];
for(int i=0; i<SHA256_DIGEST_LENGTH; i++)
sprintf(&sha256string[i*2], "%02x", (unsigned int)hash[i]);
printf("SHA256 of \"%s\" is: %s\n", str, sha256string);
return 0;
}
```
以上代码同样实现了SHA256算法,将字符串"hello world"进行哈希并输出结果。在使用openssl库之前,需要先安装并配置好。
相关问题
sha256算法 c语言
SHA-256(Secure Hash Algorithm 256位)是一种散列算法,它能将任意长度的输入(又称为消息)变换成固定长度的输出,即散列值(又称为消息摘要或散列码)。这个输出的长度固定为256位(32个字节)。SHA-256是SHA-2系列的一员,也是经过最广泛审查的散列算法之一。
下面是使用C语言实现SHA-256散列算法的一个示例:
```
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
int main(int argc, char *argv[])
{
unsigned char digest[SHA256_DIGEST_LENGTH];
char string[] = "hello, world";
SHA256((unsigned char*)&string, strlen(string), (unsigned char*)&digest);
char mdString[SHA256_DIGEST_LENGTH*2+1];
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
printf("SHA256 digest: %s\n", mdString);
return 0;
}
```
在这个示例中,我们使用了OpenSSL库中的SHA256函数来计算散列值。首先,我们将要散列的消息存储在字符串中,然后调用SHA256函数来计算散列值。最后,我们将散列值转换为十六进制字符串并打印出来。
希望这对你有帮助!
sha3算法C语言实现
以下是SHA-3算法的C语言实现,其中包括SHA3-224、SHA3-256、SHA3-384和SHA3-512。
```c
#include <stdio.h>
#include <stdint.h>
#define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y))))
const uint64_t keccakf_rndc[24] = {
0x0000000000000001, 0x0000000000008082, 0x800000000000808a,
0x8000000080008000, 0x000000000000808b, 0x0000000080000001,
0x8000000080008081, 0x8000000000008009, 0x000000000000008a,
0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
0x000000000000800a, 0x800000008000000a, 0x8000000080008081,
0x8000000000008080, 0x0000000080000001, 0x8000000080008008
};
const uint8_t keccakf_rotc[24] = {
1, 3, 6, 10, 15, 21, 28, 36,
45, 55, 2, 14, 27, 41, 56, 8,
25, 43, 62, 18, 39, 61, 20, 44
};
const uint8_t keccakf_piln[24] = {
10, 7, 11, 17, 18, 3, 5, 16,
8, 21, 24, 4, 15, 23, 19, 13,
12, 2, 20, 14, 22, 9, 6, 1
};
void keccakf(uint64_t st[25])
{
int i, j, r;
uint64_t t, bc[5];
for (r = 0; r < 24; r++) {
for (i = 0; i < 5; i++)
bc[i] = st[i] ^ st[i + 5] ^ st[i + 10] ^ st[i + 15] ^ st[i + 20];
for (i = 0; i < 5; i++) {
t = bc[(i + 4) % 5] ^ ROTL64(bc[(i + 1) % 5], 1);
for (j = 0; j < 25; j += 5)
st[j + i] ^= t;
}
t = st[1];
for (i = 0; i < 24; i++) {
j = keccakf_piln[i];
bc[0] = st[j];
st[j] = ROTL64(t, keccakf_rotc[i]);
t = bc[0];
}
for (j = 0; j < 25; j += 5) {
for (i = 0; i < 5; i++)
bc[i] = st[j + i];
for (i = 0; i < 5; i++)
st[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5];
}
st[0] ^= keccakf_rndc[r];
}
}
void sha3(const uint8_t *in, int inlen, uint8_t *md, int mdlen)
{
uint64_t st[25];
uint8_t temp[200];
int i;
for (i = 0; i < 25; i++)
st[i] = 0;
while (inlen >= 200) {
for (i = 0; i < 200 / 8; i++)
st[i] ^= ((uint64_t *)in)[i];
keccakf(st);
in += 200;
inlen -= 200;
}
for (i = 0; i < inlen; i++)
temp[i] = in[i];
temp[inlen] = 1;
inlen++;
for (i = (inlen + 16) / 17 * 17; i < 200; i++)
temp[i] = 0;
temp[200 - 1] |= 0x80;
for (i = 0; i < 200 / 8; i++)
st[i] ^= ((uint64_t *)temp)[i];
keccakf(st);
for (i = 0; i < mdlen; i++)
md[i] = st[i / 8] >> 8 * (i % 8);
}
void sha3_224(const uint8_t *in, int inlen, uint8_t *md)
{
sha3(in, inlen, md, 28);
}
void sha3_256(const uint8_t *in, int inlen, uint8_t *md)
{
sha3(in, inlen, md, 32);
}
void sha3_384(const uint8_t *in, int inlen, uint8_t *md)
{
sha3(in, inlen, md, 48);
}
void sha3_512(const uint8_t *in, int inlen, uint8_t *md)
{
sha3(in, inlen, md, 64);
}
```
这个实现中,`keccakf()`函数是Keccak-1600置换函数的实现,`sha3()`函数是SHA-3算法的主要实现,而`sha3_224()`、`sha3_256()`、`sha3_384()`和`sha3_512()`函数分别是SHA3-224、SHA3-256、SHA3-384和SHA3-512算法的实现。