本文整理汇总了C++中HMAC_CTX_init函数的典型用法代码示例。如果您正苦于以下问题:C++ HMAC_CTX_init函数的具体用法?C++ HMAC_CTX_init怎么用?C++ HMAC_CTX_init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了HMAC_CTX_init函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
// The secret key for hashing
const char key[] = "012345678";
// The data that we're going to hash
char data[] = "hello world";
// Be careful of the length of string with the choosen hash engine. SHA1 needed 20 characters.
// Change the length accordingly with your choosen hash engine.
unsigned char* result;
unsigned int len = 20;
result = (unsigned char*)malloc(sizeof(char) * len);
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
// Using sha1 hash engine here.
// You may use other hash engines. e.g EVP_md5(), EVP_sha224, EVP_sha512, etc
HMAC_Init_ex(&ctx, key, strlen(key), EVP_sha1(), NULL);
HMAC_Update(&ctx, (unsigned char*)&data, strlen(data));
HMAC_Final(&ctx, result, &len);
HMAC_CTX_cleanup(&ctx);
printf("HMAC digest: ");
for (int i = 0; i != len; i++)
printf("%02x", (unsigned int)result[i]);
printf("\n");
free(result);
return 0;
}
示例2: te_digest_init
//These functions should be replaced with VHMS API calls
int te_digest_init(EVP_MD_CTX *ctx) {
struct te_hmac_sha1_digest_ctx *c = ctx->md_data;
printf("Digest init. Current key: %s\n", c->key);
HMAC_CTX_init(&c->hctx);
HMAC_Init_ex(&c->hctx, c->key, c->key_ln, EVP_sha1(), 0);
return 1;
}
示例3: hmac_init
HMACCTX hmac_init(const void *key, int len, enum ssh_hmac_e type) {
HMACCTX ctx = NULL;
ctx = malloc(sizeof(*ctx));
if (ctx == NULL) {
return NULL;
}
#ifndef OLD_CRYPTO
HMAC_CTX_init(ctx); // openssl 0.9.7 requires it.
#endif
switch(type) {
case SSH_HMAC_SHA1:
HMAC_Init(ctx, key, len, EVP_sha1());
break;
case SSH_HMAC_MD5:
HMAC_Init(ctx, key, len, EVP_md5());
break;
default:
SAFE_FREE(ctx);
ctx = NULL;
}
return ctx;
}
示例4: CryptoNative_HmacCreate
extern "C" HMAC_CTX* CryptoNative_HmacCreate(const uint8_t* key, int32_t keyLen, const EVP_MD* md)
{
assert(key != nullptr || keyLen == 0);
assert(keyLen >= 0);
assert(md != nullptr);
std::unique_ptr<HMAC_CTX> ctx(new (std::nothrow) HMAC_CTX);
if (ctx == nullptr)
{
// Allocation failed
return nullptr;
}
// NOTE: We can't pass nullptr as empty key since HMAC_Init_ex will interpret
// that as request to reuse the "existing" key.
uint8_t _;
if (keyLen == 0)
key = &_;
HMAC_CTX_init(ctx.get());
int ret = HMAC_Init_ex(ctx.get(), key, keyLen, md, nullptr);
if (!ret)
{
return nullptr;
}
return ctx.release();
}
示例5: winpr_HMAC_Init
int winpr_HMAC_Init(WINPR_HMAC_CTX* ctx, int md, const BYTE* key, size_t keylen)
{
#if defined(WITH_OPENSSL)
const EVP_MD* evp = winpr_openssl_get_evp_md(md);
if (!evp)
return -1;
HMAC_CTX_init((HMAC_CTX*) ctx);
#if (OPENSSL_VERSION_NUMBER < 0x10000000L)
HMAC_Init_ex((HMAC_CTX*) ctx, key, keylen, evp, NULL);
#else
if (HMAC_Init_ex((HMAC_CTX*) ctx, key, keylen, evp, NULL) != 1)
return -1;
#endif
#elif defined(WITH_MBEDTLS)
const mbedtls_md_info_t* md_info;
mbedtls_md_type_t md_type = winpr_mbedtls_get_md_type(md);
md_info = mbedtls_md_info_from_type(md_type);
if (!md_info)
return -1;
mbedtls_md_init((mbedtls_md_context_t*) ctx);
if (mbedtls_md_setup((mbedtls_md_context_t*) ctx, md_info, 1) != 0)
return -1;
if (mbedtls_md_hmac_starts((mbedtls_md_context_t*) ctx, key, keylen) != 0)
return -1;
#endif
return 0;
}
示例6: HMAC_CTX_init
// Signing functions
bool OSSLEVPMacAlgorithm::signInit(const SymmetricKey* key)
{
// Call the superclass initialiser
if (!MacAlgorithm::signInit(key))
{
return false;
}
// Initialize the context
HMAC_CTX_init(&curCTX);
// Initialize EVP signing
if (!HMAC_Init(&curCTX, key->getKeyBits().const_byte_str(), key->getKeyBits().size(), getEVPHash()))
{
ERROR_MSG("HMAC_Init failed");
HMAC_CTX_cleanup(&curCTX);
ByteString dummy;
MacAlgorithm::signFinal(dummy);
return false;
}
return true;
}
示例7: hmac_fdigest
static int hmac_fdigest(lua_State *L)
{
HMAC_CTX c;
unsigned char digest[EVP_MAX_MD_SIZE];
size_t written = 0;
unsigned int i;
char *hex;
const char *t = luaL_checkstring(L, 1);
const char *s = luaL_checkstring(L, 2);
const char *k = luaL_checkstring(L, 3);
const EVP_MD *type = EVP_get_digestbyname(t);
if (type == NULL) {
luaL_argerror(L, 1, "invalid digest type");
return 0;
}
HMAC_CTX_init(&c);
HMAC_Init_ex(&c, k, lua_strlen(L, 3), type, NULL);
HMAC_Update(&c, (unsigned char *)s, lua_strlen(L, 2));
HMAC_Final(&c, digest, &written);
if (lua_toboolean(L, 4))
lua_pushlstring(L, (char *)digest, written);
else
{
hex = calloc(sizeof(char), written*2 + 1);
for (i = 0; i < written; i++)
sprintf(hex + 2*i, "%02x", digest[i]);
lua_pushlstring(L, hex, written*2);
free(hex);
}
return 1;
}
示例8: calculate_reply_hash
static krb5_error_code
calculate_reply_hash(krb5_context context,
krb5_keyblock *key,
Kx509Response *rep)
{
krb5_error_code ret;
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx, key->keyvalue.data, key->keyvalue.length,
EVP_sha1(), NULL);
ret = krb5_data_alloc(rep->hash, HMAC_size(&ctx));
if (ret) {
HMAC_CTX_cleanup(&ctx);
krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
return ENOMEM;
}
HMAC_Update(&ctx, version_2_0, sizeof(version_2_0));
if (rep->error_code) {
int32_t t = *rep->error_code;
do {
unsigned char p = (t & 0xff);
HMAC_Update(&ctx, &p, 1);
t >>= 8;
} while (t);
}
示例9: winpr_HMAC_New
WINPR_HMAC_CTX* winpr_HMAC_New(void)
{
WINPR_HMAC_CTX* ctx = NULL;
#if defined(WITH_OPENSSL)
HMAC_CTX* hmac = NULL;
#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
if (!(hmac = (HMAC_CTX*) calloc(1, sizeof(HMAC_CTX))))
return NULL;
HMAC_CTX_init(hmac);
#else
if (!(hmac = HMAC_CTX_new()))
return NULL;
#endif
ctx = (WINPR_HMAC_CTX*) hmac;
#elif defined(WITH_MBEDTLS)
mbedtls_md_context_t* hmac;
if (!(hmac = (mbedtls_md_context_t*) calloc(1, sizeof(mbedtls_md_context_t))))
return NULL;
mbedtls_md_init(hmac);
ctx = (WINPR_HMAC_CTX*) hmac;
#endif
return ctx;
}
示例10: doFile
static void doFile(FILE *fp)
{
HMAC_CTX ctx;
unsigned char digest[EVP_MAX_MD_SIZE];
unsigned int i = 0, dlen = (unsigned int)sizeof(digest);
size_t rd = 0;
const char key[] = "etaonrishdlcupfm";
unsigned char buf[4096];
/* Initialise context */
HMAC_CTX_init(&ctx);
/* Set digest type and key in context */
HMAC_Init_ex(&ctx, key, strlen(key), EVP_sha1(), NULL);
/* Process input stream */
do {
rd = fread(buf,sizeof(char),sizeof(buf),fp);
if(ferror(fp) || ((rd < sizeof(buf)) && !feof(fp))) exit(3);
if(!HMAC_Update(&ctx, buf, (unsigned int)rd)) exit(4);
} while(!feof(fp));
/* Generate digest */
if(!HMAC_Final(&ctx, digest, &dlen)) exit(5);
HMAC_CTX_cleanup(&ctx);
/* Display digest in hex */
for(i = 0; i < dlen; i++) printf("%02x", digest[i]);
printf("\n");
return;
}
示例11: memcpy
HmacHash::HmacHash()
{
uint8 temp[SEED_KEY_SIZE] = { 0x38, 0xA7, 0x83, 0x15, 0xF8, 0x92, 0x25, 0x30, 0x71, 0x98, 0x67, 0xB1, 0x8C, 0x4, 0xE2, 0xAA };
memcpy(&m_key, &temp, SEED_KEY_SIZE);
HMAC_CTX_init(&m_ctx);
HMAC_Init_ex(&m_ctx, &m_key, SEED_KEY_SIZE, EVP_sha1(), NULL);
}
示例12: verify_req_hash
static krb5_error_code
verify_req_hash(krb5_context context,
const Kx509Request *req,
krb5_keyblock *key)
{
unsigned char digest[SHA_DIGEST_LENGTH];
HMAC_CTX ctx;
if (req->pk_hash.length != sizeof(digest)) {
krb5_set_error_message(context, KRB5KDC_ERR_PREAUTH_FAILED,
"pk-hash have wrong length: %lu",
(unsigned long)req->pk_hash.length);
return KRB5KDC_ERR_PREAUTH_FAILED;
}
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx,
key->keyvalue.data, key->keyvalue.length,
EVP_sha1(), NULL);
if (sizeof(digest) != HMAC_size(&ctx))
krb5_abortx(context, "runtime error, hmac buffer wrong size in kx509");
HMAC_Update(&ctx, version_2_0, sizeof(version_2_0));
HMAC_Update(&ctx, req->pk_key.data, req->pk_key.length);
HMAC_Final(&ctx, digest, 0);
HMAC_CTX_cleanup(&ctx);
if (memcmp(req->pk_hash.data, digest, sizeof(digest)) != 0) {
krb5_set_error_message(context, KRB5KDC_ERR_PREAUTH_FAILED,
"pk-hash is not correct");
return KRB5KDC_ERR_PREAUTH_FAILED;
}
return 0;
}
示例13: rtmp_handshake_make_digest
static int rtmp_handshake_make_digest(const uint8_t* key, size_t len, const uint8_t* ptr, size_t ptrlen, const uint8_t* digest, uint8_t* dst)
{
static HMAC_CTX *hmac;
unsigned int digestlen;
if (hmac == NULL) {
#if OPENSSL_VERSION_NUMBER < 0x10100000L
static HMAC_CTX shmac;
hmac = &shmac;
HMAC_CTX_init(hmac);
#else
hmac = HMAC_CTX_new();
#endif
}
HMAC_Init_ex(hmac, key, len, EVP_sha256(), NULL);
if (digest)
{
assert(digest + SHA256_DIGEST_LENGTH <= ptr + ptrlen);
HMAC_Update(hmac, ptr, digest - ptr);
if (digest + SHA256_DIGEST_LENGTH < ptr + ptrlen)
HMAC_Update(hmac, digest + SHA256_DIGEST_LENGTH, ptrlen - (digest - ptr) - SHA256_DIGEST_LENGTH);
}
else
{
HMAC_Update(hmac, ptr, ptrlen);
}
HMAC_Final(hmac, dst, &digestlen);
assert(digestlen == SHA256_DIGEST_LENGTH);
return 0;
}
示例14: check_hmac
static void
check_hmac(void)
{
unsigned char buf[4] = { 0, 0, 0, 0 };
char hmackey[] = "hello-world";
size_t hmackey_size = sizeof(hmackey);
unsigned int hmaclen;
unsigned char hmac[EVP_MAX_MD_SIZE];
HMAC_CTX c;
char answer[20] = "\x2c\xfa\x32\xb7\x2b\x8a\xf6\xdf\xcf\xda"
"\x6f\xd1\x52\x4d\x54\x58\x73\x0f\xf3\x24";
HMAC_CTX_init(&c);
HMAC_Init_ex(&c, hmackey, hmackey_size, EVP_sha1(), NULL);
HMAC_Update(&c, buf, sizeof(buf));
HMAC_Final(&c, hmac, &hmaclen);
HMAC_CTX_cleanup(&c);
if (hmaclen != 20)
errx(1, "hmaclen = %d\n", (int)hmaclen);
if (ct_memcmp(hmac, answer, hmaclen) != 0)
errx(1, "wrong answer\n");
}
示例15: OPENSSL_malloc
static HMAC_CTX *HMAC_CTX_new(void)
{
HMAC_CTX *ctx = OPENSSL_malloc(sizeof(HMAC_CTX));
if (ctx != NULL)
HMAC_CTX_init(ctx);
return ctx;
}