用 Ruby of a Bignum 计算 SHA1 摘要
我有这段 C 代码,它使用 openssl 库来计算大数的 SHA1 摘要。 我如何用 Ruby 翻译这段代码?
#include <stdio.h>
#include <openssl/sha.h>
#include <openssl/bn.h>
int
main ()
{
// Create a bignum = 3
struct bignum_st *bn = BN_new ();
BN_set_word (bn, 3);
// Initialize SHA1 context
unsigned char digest[SHA_DIGEST_LENGTH];
SHA_CTX sha_ctx;
SHA1_Init (&sha_ctx);
// Calculate SHA1 digest (in binary) of bignum 3
SHA1_Update (&sha_ctx, bn, BN_num_bytes (bn));
SHA1_Final (digest, &sha_ctx);
// Print HEX value of digest
int i;
for (i = 0; i < SHA_DIGEST_LENGTH; i++)
printf ("%x", digest[i]);
printf ("\n");
// result is: 516b9783fca517eecbd1d064da2d165310b19759
}
编译:gcc sha1.c -lcrypto
谢谢。
I have this code in C that use openssl library to calculate the SHA1 digest of a bignumber.
How I can translate this code in Ruby?
#include <stdio.h>
#include <openssl/sha.h>
#include <openssl/bn.h>
int
main ()
{
// Create a bignum = 3
struct bignum_st *bn = BN_new ();
BN_set_word (bn, 3);
// Initialize SHA1 context
unsigned char digest[SHA_DIGEST_LENGTH];
SHA_CTX sha_ctx;
SHA1_Init (&sha_ctx);
// Calculate SHA1 digest (in binary) of bignum 3
SHA1_Update (&sha_ctx, bn, BN_num_bytes (bn));
SHA1_Final (digest, &sha_ctx);
// Print HEX value of digest
int i;
for (i = 0; i < SHA_DIGEST_LENGTH; i++)
printf ("%x", digest[i]);
printf ("\n");
// result is: 516b9783fca517eecbd1d064da2d165310b19759
}
Compile with: gcc sha1.c -lcrypto
Thx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论