C++实现X962SHA256
时间: 2024-01-10 16:19:31 浏览: 173
以下是C++实现X962 SHA256的代码示例:
```cpp
#include <iostream>
#include <cstring>
#include <openssl/sha.h>
using namespace std;
int main() {
// 待哈希的数据
unsigned char data[] = "Hello, world!";
// 哈希结果
unsigned char hash[SHA256_DIGEST_LENGTH];
// 计算哈希值
SHA256(data, strlen((char*)data), hash);
// 输出哈希结果
cout << "SHA256 hash: ";
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
printf("%02x", hash[i]);
}
cout << endl;
return 0;
}
```
相关问题
sha256生成RSA密匙对的C++实现
以下是基于OpenSSL库的C++代码实现:
```cpp
#include <openssl/rsa.h>
#include <openssl/sha.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
void generateRSAKeyPair(char* publicKey, char* privateKey) {
RSA *rsa = RSA_new();
BIGNUM *bn = BN_new();
SHA256_CTX sha256;
unsigned char hash[SHA256_DIGEST_LENGTH];
char hexHash[SHA256_DIGEST_LENGTH * 2 + 1];
BIO *bio = NULL;
int ret;
// 生成RSA密钥对
BN_set_word(bn, RSA_F4);
RSA_generate_key_ex(rsa, 2048, bn, NULL);
BN_free(bn);
// 生成公钥PEM文件
bio = BIO_new(BIO_s_mem());
ret = PEM_write_bio_RSAPublicKey(bio, rsa);
if (ret != 1) {
printf("Error: Failed to write public key!\n");
RSA_free(rsa);
BIO_free(bio);
return;
}
ret = BIO_flush(bio);
if (ret != 1) {
printf("Error: Failed to flush BIO!\n");
RSA_free(rsa);
BIO_free(bio);
return;
}
ret = BIO_get_mem_data(bio, &publicKey);
if (ret <= 0) {
printf("Error: Failed to get public key data from BIO!\n");
RSA_free(rsa);
BIO_free(bio);
return;
}
publicKey[ret] = '\0';
BIO_free(bio);
// 生成私钥PEM文件
bio = BIO_new(BIO_s_mem());
ret = PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, NULL);
if (ret != 1) {
printf("Error: Failed to write private key!\n");
RSA_free(rsa);
BIO_free(bio);
return;
}
ret = BIO_flush(bio);
if (ret != 1) {
printf("Error: Failed to flush BIO!\n");
RSA_free(rsa);
BIO_free(bio);
return;
}
ret = BIO_get_mem_data(bio, &privateKey);
if (ret <= 0) {
printf("Error: Failed to get private key data from BIO!\n");
RSA_free(rsa);
BIO_free(bio);
return;
}
privateKey[ret] = '\0';
BIO_free(bio);
// 计算公钥SHA256哈希值
SHA256_Init(&sha256);
ret = i2d_RSAPublicKey(rsa, NULL);
if (ret <= 0) {
printf("Error: Failed to get public key data length!\n");
RSA_free(rsa);
return;
}
unsigned char *publicKeyData = (unsigned char *)malloc(ret);
ret = i2d_RSAPublicKey(rsa, &publicKeyData);
if (ret <= 0) {
printf("Error: Failed to get public key data!\n");
RSA_free(rsa);
free(publicKeyData);
return;
}
SHA256_Update(&sha256, publicKeyData, ret);
free(publicKeyData);
SHA256_Final(hash, &sha256);
// 将SHA256哈希值转换为十六进制字符串
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
sprintf(hexHash + i * 2, "%02x", hash[i]);
}
hexHash[SHA256_DIGEST_LENGTH * 2] = '\0';
printf("Public Key:\n%s\n", publicKey);
printf("Private Key:\n%s\n", privateKey);
printf("Public Key SHA256 Hash:\n%s\n", hexHash);
RSA_free(rsa);
}
```
其中,`generateRSAKeyPair`函数接受两个`char`类型的指针参数,用于返回生成的公钥和私钥PEM文件内容。该函数实现了以下操作:
1. 生成2048位RSA密钥对;
2. 将公钥PEM文件内容写入内存`BIO`中,并获取其数据;
3. 将私钥PEM文件内容写入内存`BIO`中,并获取其数据;
4. 计算公钥的SHA256哈希值,并将其转换为十六进制字符串;
5. 打印公钥、私钥和SHA256哈希值。
需要注意的是,该实现使用了OpenSSL库中的函数,因此需要在编译时链接OpenSSL库。
SHA256算法C++
您好!SHA-256(Secure Hash Algorithm 256-bit)是一种常用的哈希算法,用于将任意长度的数据转换为固定长度(256位)的哈希值。以下是一个使用C语言实现SHA-256算法的示例代码:
```c
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
uint32_t ch(uint32_t x, uint32_t y, uint32_t z) {
return (x & y) ^ (~x & z);
}
uint32_t maj(uint32_t x, uint32_t y, uint32_t z) {
return (x & y) ^ (x & z) ^ (y & z);
}
uint32_t ep0(uint32_t x) {
return ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22);
}
uint32_t ep1(uint32_t x) {
return ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25);
}
uint32_t sig0(uint32_t x) {
return ROTR(x, 7) ^ ROTR(x, 18) ^ (x >> 3);
}
uint32_t sig1(uint32_t x) {
return ROTR(x, 17) ^ ROTR(x, 19) ^ (x >> 10);
}
void sha256(uint8_t *message, size_t len, uint8_t *hash) {
uint32_t h[8] = {
0x6a09e667,
0xbb67ae85,
0x3c6ef372,
0xa54ff53a,
0x510e527f,
0x9b05688c,
0x1f83d9ab,
0x5be0cd19
};
uint32_t k[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0xfc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x6ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
size_t new_len = ((((len + 8) / 64) + 1) * 64) - 8;
uint8_t *msg = (uint8_t *)malloc(new_len + 64);
memcpy(msg, message, len);
msg[len] = 128;
for (size_t i = len + 1; i < new_len; ++i) {
msg[i] = 0;
}
uint64_t bit_len = len * 8;
memcpy(msg + new_len, &bit_len, sizeof(uint64_t));
for (size_t offset = 0; offset < new_len; offset += 64) {
uint32_t w[64];
for (size_t i = 0; i < 16; ++i) {
w[i] = (msg[offset + i * 4] << 24) |
(msg[offset + i * 4 + 1] << 16) |
(msg[offset + i * 4 + 2] << 8) |
(msg[offset + i * 4 + 3]);
}
for (size_t i = 16; i < 64; ++i) {
w[i] = sig1(w[i - 2]) + w[i - 7] + sig0(w[i - 15]) + w[i - 16];
}
uint32_t a = h[0];
uint32_t b = h[1];
uint32_t c = h[2];
uint32_t d = h[3];
uint32_t e = h[4];
uint32_t f = h[5];
uint32_t g = h[6];
uint32_t h_ = h[7];
for (size_t i = 0; i < 64; ++i) {
uint32_t t1 = h_ + ep1(e) + ch(e, f, g) + k[i] + w[i];
uint32_t t2 = ep0(a) + maj(a, b, c);
h_ = g;
g = f;
f = e;
e = d + t1;
d = c;
c = b;
b = a;
a = t1 + t2;
}
h[0] += a;
h[1] += b;
h[2] += c;
h[3] += d;
h[4] += e;
h[5] += f;
h[6] += g;
h[7] += h_;
}
for (size_t i = 0; i < 8; ++i) {
hash[i * 4] = (h[i] >> 24) & 0xFF;
hash[i * 4 + 1] = (h[i] >> 16) & 0xFF;
hash[i * 4 + 2] = (h[i] >> 8) & 0xFF;
hash[i * 4 + 3] = h[i] & 0xFF;
}
free(msg);
}
int main() {
uint8_t message[] = "Hello, world!";
uint8_t hash[32];
sha256(message, sizeof(message) - 1, hash);
printf("SHA-256 Hash: ");
for (size_t i = 0; i < 32; ++i) {
printf("%02x", hash[i]);
}
printf("\n");
return 0;
}
```
这段代码实现了SHA-256算法的核心部分,可以将输入的消息(message)转换为对应的SHA-256哈希值。运行示例代码会输出消息 "Hello, world!" 的SHA-256哈希值。请注意,此处的代码仅作为参考,实际应用中建议使用经过充分测试和验证的库或工具来计算SHA-256哈希。
阅读全文