• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ HMAC函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中HMAC函数的典型用法代码示例。如果您正苦于以下问题:C++ HMAC函数的具体用法?C++ HMAC怎么用?C++ HMAC使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了HMAC函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: eval

	std::string eval(const std::string& key, const std::string& data)
	{
		std::string out(evp_->md_size + 1, 0);
		unsigned int outLen = 0;
		if (HMAC(evp_, key.c_str(), static_cast<int>(key.size()),
			cybozu::cast<const uint8_t *>(data.c_str()), data.size(), cybozu::cast<uint8_t *>(&out[0]), &outLen)) {
			out.resize(outLen);
			return out;
		}
		throw cybozu::Exception("crypto::Hamc::eval");
	}
开发者ID:pombredanne,项目名称:cybozulib,代码行数:11,代码来源:crypto.hpp


示例2: strlen

char *hmacSha1(char *key, char *data)
/* Calculate a openssl SHA1 keyed-hash message authentication code (HMAC) */
{
unsigned char* digest;
digest=HMAC(EVP_sha1(), key, strlen(key), (unsigned char*)data, strlen(data), NULL, NULL);
char hmacStr[40];
int i;
for(i = 0; i < 20; i++)
    sprintf(&hmacStr[i*2], "%02x", (unsigned int)digest[i]);
return cloneStringZ(hmacStr, sizeof(hmacStr));
}
开发者ID:elmargb,项目名称:kentUtils,代码行数:11,代码来源:hmac.c


示例3: openvpn_encrypt

/*
 * --tls-auth is read in do_init_crypto_tls_c1() in src/vpn/init.c
 * by calling get_tls_handshake_key()
 * init_key_ctx()
 */
openvpn_encrypt(ovStruct_t *ovP, uchar *ptr, int length, int hmac_index) {
	uchar tmpPtr[5000];
	int tmpLen, i;
	uchar *hash;

	// 5th line for outgoing from server
	//uchar key[] = "\x25\x21\x1f\x2f\x4e\x2a\x50\x0d\x13\x3f\x19\xe2\x4c\xd5\xf5\x06\xc0\xa7\xe6\xf0";
	// 13th line for incoming into server
	uchar key[] = "\xac\x3c\x20\xbb\xb7\x54\x8d\x9a\x8d\x9c\x9f\xdd\x76\xde\x22\x14\x25\xfc\xcc\x07";

	memcpy(tmpPtr, ptr, length);
	memcpy(&tmpPtr[28], &tmpPtr[0], 9);
	tmpLen = length-28;
	// Copy pkt id + timestamp to the start of the pkt
	memcpy(&tmpPtr[20], &ptr[29], 8);
	tmpLen += 8;
	log_info(fp, "openvpn_encrypt: HMAC at:%d in pkt of len:%d, newlen:%d",
			hmac_index, length, tmpLen);
	fflush(fp);
	// Note that both the following HMAC versions work. Either way can be used.
	// Both have been tested with the openvpn_as server.
	{
	hash = HMAC(EVP_sha1(), key, strlen(key), &tmpPtr[20], tmpLen, NULL, NULL);
	}
	/*
	{
	unsigned char hash[SHA_DIGEST_LENGTH];
	uchar *output = NULL;
    HMAC_CTX hmac;
    unsigned int in_hmac_len = 0;
	ENGINE_load_builtin_engines();
	ENGINE_register_all_complete();

	HMAC_CTX_init(&hmac);
    HMAC_Init_ex(&hmac, key, 20, EVP_sha1(), NULL);
    HMAC_Update(&hmac,  &tmpPtr[20], tmpLen);
    HMAC_Final(&hmac, hash, &in_hmac_len);
    HMAC_CTX_cleanup(&hmac);
	}*/
	
	// hash now contains the 20-byte SHA-1 hash
	memcpy(&ptr[hmac_index], hash, SHA_DIGEST_LENGTH);
#if DEBUG
	printf("\n HMAC KEY: ");
	for (i=0;i<20;i++)
		printf("%2x ",key[i]);
	printf("\n HMAC ON DATA: ");
	for (i=0;i<tmpLen;i++)
		printf("%2x ",tmpPtr[20+i]);
	printf("\n HMAC SHA1: ");
	for (i=0;i<20;i++)
		printf("%2x ",hash[i]);
#endif
}
开发者ID:aseemsethi,项目名称:monitor,代码行数:59,代码来源:openvpn.c


示例4: nr_ice_crypto_openssl_hmac_sha1

static int nr_ice_crypto_openssl_hmac_sha1(UCHAR *key, int key_l, UCHAR *buf, int buf_l, UCHAR digest[20]) {
  unsigned int rl;

  HMAC(EVP_sha1(),
    key, key_l, buf, buf_l, digest, &rl);

  if (rl != 20)
    ERETURN(R_INTERNAL);

  return 0;
}
开发者ID:notedit,项目名称:licode,代码行数:11,代码来源:NicerConnection.cpp


示例5: HMAC

char *s3_sign_string(const S3 *s3,const char *string)
{
	unsigned char md[EVP_MAX_MD_SIZE];
	unsigned int md_len;

	if(!s3) return NULL;

	HMAC(EVP_sha1(),s3->secret_key,strlen(s3->secret_key),
		(unsigned char *)string,strlen(string),md,&md_len);
	return ne_base64(md,md_len);
}
开发者ID:soundsrc,项目名称:s3nbd,代码行数:11,代码来源:libs3.c


示例6: DeriveKey

int32 DeriveKey(const void *key, int32 keyLen, const uchar *magic, int32 magicLen, uchar **result) {
	uchar hash1[EVP_MAX_MD_SIZE];
	uchar hash2[EVP_MAX_MD_SIZE];
	uchar hash3[EVP_MAX_MD_SIZE];
	uchar hash4[EVP_MAX_MD_SIZE];
	unsigned int hash1Len = 0;
	unsigned int hash2Len = 0;
	unsigned int hash3Len = 0;
	unsigned int hash4Len = 0;
	int32 length = B_ERROR;
	BMallocIO temp;
	
	// HMAC-SHA1(magic)
	HMAC(EVP_sha1(), key, keyLen, magic, magicLen, hash1, &hash1Len);

	// Key 2 is HMAC-SHA1(HMAC-SHA1(magic) + magic)
	temp.Write(hash1, hash1Len);
	temp.Write(magic, magicLen);
	HMAC(EVP_sha1(), key, keyLen, (uchar *)temp.Buffer(), temp.BufferLength(), hash2, &hash2Len);

	// HMAC-SHA1(HMAC-SHA1(magic))
	HMAC(EVP_sha1(), key, keyLen, hash1, hash1Len, hash3, &hash3Len);
			
	// Clear the BMallocIO and reset the position to 0
	temp.SetSize(0);
	temp.Seek(0, SEEK_SET);

	// Key 4 is HMAC-SHA1(HMAC-SHA1(HMAC-SHA1(magic)) + magic)
	temp.Write(hash3, hash3Len);
	temp.Write(magic, magicLen);
	HMAC(EVP_sha1(), key, keyLen, (uchar *)temp.Buffer(), temp.BufferLength(), hash4, &hash4Len);

	// The key is Hash2 followed by the first four bytes of Hash4
	length = hash2Len + 4;
	*result = (uchar *)calloc(length, sizeof(uchar));

	memcpy(*result, hash2, hash2Len);
	memcpy(*result + hash2Len, hash4, 4);
	
	return length;
};
开发者ID:HaikuArchives,项目名称:IMKit,代码行数:41,代码来源:SSO.cpp


示例7: anonymization_process

/* Anonymize a buffer of given length. Places the resulting digest into the
 * provided digest buffer, which must be at least ANONYMIZATION_DIGEST_LENGTH
 * bytes long. */
static int anonymization_process(const uint8_t* const data,
                                 const int len,
                                 unsigned char* const digest) {
#ifndef NDEBUG
  assert(initialized);
#endif

  if (!HMAC(EVP_sha1(), seed, ANONYMIZATION_SEED_LEN, data, len, digest, NULL)) {
    return -1;
  }
  return 0;
}
开发者ID:woodrow,项目名称:bismark-passive,代码行数:15,代码来源:anonymization.c


示例8: zauthToken

char * zauthToken(unsigned long int gid, unsigned long int zid,time_t exp_time)
{
        char str[MAX],str_final[MAX],temp[MAX],str_sig[MAX];
         int len,len1;
        char *ret = (char *) malloc(1024 * sizeof(char *));
        sprintf(str, "%lu:%lu.%lu.AAAAAA==",gid,zid,(unsigned long int)exp_time);
        unsigned char *ptr = HMAC(EVP_sha256(),my.secret,strlen(my.secret),str,strlen(str),str_final,&len); // hmac--sha256 before base64 encoding....

        base64_enc(str_final,len,1,str_sig);                                                                    //base64 encoding to get signature
        sprintf(ret, "%s|%s\n",str,str_sig);
	return ret;
}
开发者ID:muttu2244,项目名称:MyPython,代码行数:12,代码来源:util.c


示例9: crypto_kdcmsg_cksum_hmacmd5

rdpBlob* crypto_kdcmsg_cksum_hmacmd5(rdpBlob* msg, uint8* key, uint32 msgtype)
{
	rdpBlob* cksum;
	uint8* Ksign;
	uint8* tmpdata;
	uint8* tmp;
	CryptoMd5 md5;
	Ksign = xzalloc(16);
	tmp = xzalloc(16);
	cksum = xnew(rdpBlob);
	freerdp_blob_alloc(cksum, 16);
	tmpdata = xzalloc(msg->length + 4);
	HMAC(EVP_md5(), (void*) key, 16, (uint8*)"signaturekey\0", 13, (void*) Ksign, NULL);
	memcpy(tmpdata, (void*)&msgtype, 4);
	memcpy(tmpdata + 4, msg->data, msg->length);
	md5 = crypto_md5_init();
	crypto_md5_update(md5, tmpdata, msg->length + 4);
	crypto_md5_final(md5, tmp);
	HMAC(EVP_md5(), (void*) Ksign, 16, (uint8*)tmp, 16, (void*) cksum->data, NULL);
	return cksum;
}
开发者ID:littlejawa,项目名称:FreeRDP,代码行数:21,代码来源:kerberos_crypto.c


示例10: decrypt_and_verify_secrets

int decrypt_and_verify_secrets(CPOR_key *key, unsigned char *input, size_t input_len, unsigned char *plaintext, size_t *plaintext_len, unsigned char *authenticator, size_t authenticator_len) {

    EVP_CIPHER_CTX ctx;
    EVP_CIPHER *cipher = NULL;
    unsigned char mac[EVP_MAX_MD_SIZE];
    size_t mac_size = EVP_MAX_MD_SIZE;
    int len;

    if(!key || !key->k_enc || !key->k_mac || !input || !input_len || !plaintext || !plaintext_len || !authenticator || !authenticator_len) return 0;

    OpenSSL_add_all_algorithms();
    memset(mac, 0, mac_size);

    /* Verify the HMAC-SHA1 */
    if(!HMAC(EVP_sha1(), key->k_mac, key->k_mac_size, input, input_len, mac, (unsigned int *)&mac_size)) goto cleanup;
    if(authenticator_len != mac_size) goto cleanup;
    if(memcmp(mac, authenticator, mac_size) != 0) goto cleanup;


    EVP_CIPHER_CTX_init(&ctx);
    switch(key->k_enc_size) {
    case 16:
        cipher = (EVP_CIPHER *)EVP_aes_128_cbc();
        break;
    case 24:
        cipher = (EVP_CIPHER *)EVP_aes_192_cbc();
        break;
    case 32:
        cipher = (EVP_CIPHER *)EVP_aes_256_cbc();
        break;
    default:
        return 0;
    }
    if(!EVP_DecryptInit(&ctx, cipher, key->k_enc, NULL)) goto cleanup;

    *plaintext_len = 0;

    if(!EVP_DecryptUpdate(&ctx, plaintext, (int *)plaintext_len, input, input_len)) goto cleanup;
    EVP_DecryptFinal(&ctx, plaintext + *plaintext_len, &len);

    *plaintext_len += len;

    EVP_CIPHER_CTX_cleanup(&ctx);

    return 1;

cleanup:
    *plaintext_len = 0;

    return 0;

}
开发者ID:nypgit,项目名称:compact-proofs-of-retrievability,代码行数:52,代码来源:cpor-misc.c


示例11: main

int main(int argc, char **argv)
{
    Octstr *data, *filename, *mac, *key;
    unsigned char macbuf[EVP_MAX_MD_SIZE], *p;
    int mac_len;
#ifdef HAVE_LIBSSL
    HMAC_CTX ctx;
#endif

    gwlib_init();

    get_and_set_debugs(argc, argv, NULL);

    if (argc < 3)
        panic(0, "Syntax: %s <key> <file>\n", argv[0]);
  
    key = octstr_create(argv[1]);    
    filename = octstr_create(argv[2]);
    data = octstr_read_file(octstr_get_cstr(filename));

    if (data == NULL)
        panic(0, "Cannot read file.");

    debug("",0,"Dumping file `%s':", octstr_get_cstr(filename));
    octstr_dump(data, 0);

#ifdef HAVE_LIBSSL
    HMAC_Init(&ctx, octstr_get_cstr(key), octstr_len(key), EVP_sha1());
    p = HMAC(EVP_sha1(), octstr_get_cstr(key), octstr_len(key), 
         octstr_get_cstr(data), octstr_len(data), 
         macbuf, &mac_len);
    HMAC_cleanup(&ctx);
#else
    macbuf[0] = 0;
    mac_len = 0;
    p = macbuf;
    warning(0, "No SSL support. Can't calculate HMAC value.");
#endif
    
    mac = octstr_create_from_data(p, mac_len);
    octstr_binary_to_hex(mac, 0);
    
    debug("",0,"HMAC of file `%s' and key `%s' is:", 
          octstr_get_cstr(filename), octstr_get_cstr(key));
    octstr_dump(mac, 0);      

    octstr_destroy(data);
    octstr_destroy(mac);
    octstr_destroy(key);
    gwlib_shutdown();
    return 0;
}
开发者ID:gburiticato,项目名称:kannel,代码行数:52,代码来源:test_hmac.c


示例12: rdssl_hmac_md5

void
rdssl_hmac_md5(const void *key, int key_len, const unsigned char *msg, int msg_len,
	       unsigned char *md)
{
#if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(LIBRESSL_VERSION_NUMBER)
	HMAC_CTX ctx;
	HMAC_CTX_init(&ctx);
#endif
	HMAC(EVP_md5(), key, key_len, msg, msg_len, md, NULL);
#if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(LIBRESSL_VERSION_NUMBER)
	HMAC_CTX_cleanup(&ctx);
#endif
}
开发者ID:mdaniel,项目名称:virtualbox-org-svn-vbox-trunk,代码行数:13,代码来源:ssl.c


示例13:

static unsigned char *HKDF_Extract(const EVP_MD *evp_md,
                                   const unsigned char *salt, size_t salt_len,
                                   const unsigned char *key, size_t key_len,
                                   unsigned char *prk, size_t *prk_len)
{
    unsigned int tmp_len;

    if (!HMAC(evp_md, salt, salt_len, key, key_len, prk, &tmp_len))
        return NULL;

    *prk_len = tmp_len;
    return prk;
}
开发者ID:Castaglia,项目名称:openssl,代码行数:13,代码来源:hkdf.c


示例14: computeHMAC

std::string computeHMAC(const std::string& input, const std::string& key) 
{	
    //DebugL << "Compute HMAC: input='" << util::dumpbin(input.c_str(), input.length()) 
	//	<< "', inputLength=" << input.length() << ", key='" << key << "', keyLength=" << key.length() << std::endl;
	unsigned int len = 0;
	char buf[20];	
	HMAC(EVP_sha1(), 
		key.c_str(), key.length(), 
        reinterpret_cast<const unsigned char*>(input.c_str()), input.length(), 
        reinterpret_cast<unsigned char*>(&buf), &len);
	assert(len == 20);
	return std::string(buf, len);
}
开发者ID:AsamQi,项目名称:libsourcey,代码行数:13,代码来源:hmac.cpp


示例15: hmacSha1

 /*
  * Computes a HMAC SHA-1 keyed hash of 'input' using the key 'key'
  */
 bool hmacSha1(const unsigned char* key,
               const size_t keyLen,
               const unsigned char* input,
               const size_t inputLen,
               unsigned char* output,
               unsigned int* outputLen) {
     return HMAC(EVP_sha1(),
                 key,
                 keyLen,
                 input,
                 inputLen,
                 output,
                 outputLen);
 }
开发者ID:ForNowForever,项目名称:mongo,代码行数:17,代码来源:crypto_openssl.cpp


示例16: ngx_apikey_hamac_sha1_digest

/*
 * Calculate the HMAC-SHA1 digest for a specified message with a defined key.
 *
 *	@parameters:	*pool		pool for memory allocation;
 *					*key		the hmac key;
 *					*message	the message to digest;
 *	
 *	@return	NGX_ERROR 	on error
 *			NGX_OK		on success
 */
static ngx_int_t
ngx_apikey_hamac_sha1_digest( ngx_pool_t *pool, ngx_str_t *key, ngx_str_t *message, ngx_str_t *digest ) {

	ngx_str_t	hmac_digest		= ngx_null_string;

	u_char		*last			= NULL;
	

	hmac_digest.len 	= EVP_MAX_MD_SIZE;
	hmac_digest.data = ngx_pcalloc( pool, EVP_MAX_MD_SIZE );
	if ( hmac_digest.data == NULL ) {
		ngx_log_error(NGX_LOG_ERR, pool->log, 0, "(%s) failed allocating memory", __func__ );
        return NGX_ERROR;
	}

	/*
	HMAC_CTX ctx;
	HMAC_Init( &ctx, key->data, sizeof(key->data), EVP_sha1() );
	HMAC_Update(&ctx,  message->data, sizeof(message->data));
 	HMAC_Final(&ctx, hmac_digest.data, &hmac_digest.len );		
	HMAC_cleanup(&ctx);
	*/
	
	last = HMAC (EVP_sha1 (), 
		(const void *)key->data, (int)key->len, 
		message->data, (int)message->len, 
		hmac_digest.data, (unsigned int *)&hmac_digest.len );

	if ( last == NULL ) {
		ngx_pfree( pool, hmac_digest.data );
		ngx_log_error(NGX_LOG_ERR, pool->log, 0, "(%s) failed calculating HMAC digest", __func__ );
        return NGX_ERROR;
	}
	
	digest->len  = EVP_MAX_MD_SIZE*2;
	digest->data = ngx_pcalloc( pool, digest->len + 1 );
    if ( digest->data == NULL ) {
		ngx_pfree( pool, hmac_digest.data );
		ngx_log_error(NGX_LOG_ERR, pool->log, 0, "(%s) failed allocating memory", __func__ );
        return NGX_ERROR;
	}
	
	ngx_hex_dump(digest->data, hmac_digest.data, hmac_digest.len);
	

	ngx_pfree( pool, hmac_digest.data );

	
	return NGX_OK;
}
开发者ID:lucasepe,项目名称:nginx_apikey_access_filter_module,代码行数:60,代码来源:ngx_apikey_access_filter_module.c


示例17: if

int Peer::attachHash(const void *data_begin, unsigned int data_len, void *hash_begin){
	//we suppose the hash value takes 20 bytes, if the length is not 20 bytes, return 1, if fail to generate hash, return -1
	unsigned int hash_len;
	if(HMAC(EVP_sha1(), this->key, this->key_len, (const unsigned char*)data_begin, data_len, (unsigned char *)hash_begin, &hash_len) == NULL){
			Helper::LiveWithUserMessage("HMAC()", "fail to generate server-side hash");
			return -1;
	}
	else if(hash_len != 20){
		Helper::LiveWithUserMessage("HMAC()", "generate an abnormal sha1(length not 20)");
		return 1;
	}
	else
		return 0;
}
开发者ID:archiezhao,项目名称:BitTorrent-Client,代码行数:14,代码来源:Peer.cpp


示例18: hmac

K hmac(K x,K y,K f) {
    int lenx,leny,lenf,i;
    lenx=x->n;
    leny=y->n;
    lenf=f->n;
    unsigned char secret[lenx+1];
    unsigned char message[leny+1];
    unsigned char hashfunction[lenf+1];

    // copy x and y into regular cstrings
    if(10==(x->t)){ for(i=0;i<lenx;i++){ secret[i]      =kC(x)[i]; } secret[lenx]=0;       }
    if(10==(y->t)){ for(i=0;i<leny;i++){ message[i]     =kC(y)[i]; } message[leny]=0;      }
    if(10==(f->t)){ for(i=0;i<lenf;i++){ hashfunction[i]=kC(f)[i]; } hashfunction[lenf]=0; }

    unsigned int bytelength;
    const EVP_MD* (*evp_fn)(void);
    if(strcmp("sha1",hashfunction)==0){
        bytelength=SHA_DIGEST_LENGTH;
        evp_fn=&EVP_sha1;
    } else if(strcmp("sha224",hashfunction)==0){
        bytelength=SHA224_DIGEST_LENGTH;
        evp_fn=&EVP_sha224;
    } else if(strcmp("sha256",hashfunction)==0){
        bytelength=SHA256_DIGEST_LENGTH;
        evp_fn=&EVP_sha256;
    } else if(strcmp("sha384",hashfunction)==0){
        bytelength=SHA384_DIGEST_LENGTH;
        evp_fn=&EVP_sha384;
    } else if(strcmp("sha512",hashfunction)==0){
        bytelength=SHA512_DIGEST_LENGTH;
        evp_fn=&EVP_sha512;
    } else if(strcmp("md5",hashfunction)==0){
        bytelength=MD5_DIGEST_LENGTH;
        evp_fn=&EVP_md5;
    } else{
        krr("Please choose a supported hash function");
        return (K)0;
    }

    unsigned char* result;
    result=calloc(bytelength,sizeof(char));
    HMAC(evp_fn(),secret,strlen(secret),message,strlen(message),result,NULL);

    K output=ktn(KG,bytelength);
    for(i=0;i<bytelength;i++){
        kG(output)[i]=result[i];
    }
    free(result);
    return output;
}
开发者ID:tjcelaya,项目名称:qcrypt,代码行数:50,代码来源:qcrypt.c


示例19: OpcUa_P_OpenSSL_PSHA256_Hash_Generate

/* internal function */
OpcUa_StatusCode OpcUa_P_OpenSSL_PSHA256_Hash_Generate(
    OpcUa_P_OpenSSL_PSHA256_Ctx* a_pPsha256Context,
    OpcUa_Byte*              a_pHash)
{
    OpcUa_InitializeStatus(OpcUa_Module_P_OpenSSL, "PSHA256_Hash_Generate");

    OpcUa_ReturnErrorIfArgumentNull(a_pPsha256Context);

    /* Calculate P_SHA256(n) = HMAC_SHA256(secret, A(n)+seed) */
    HMAC(EVP_sha256(), OpcUa_P_OpenSSL_PSHA256_SECRET(a_pPsha256Context), a_pPsha256Context->secret_len,
                     (unsigned char*)a_pPsha256Context->A, sizeof(a_pPsha256Context->A) + a_pPsha256Context->seed_len,
                     a_pHash, OpcUa_Null);

    /* Calculate A(n) = HMAC_SHA256(secret, A(n-1)) */
    HMAC(EVP_sha256(), OpcUa_P_OpenSSL_PSHA256_SECRET(a_pPsha256Context), a_pPsha256Context->secret_len, (const unsigned char*)a_pPsha256Context->A, sizeof(a_pPsha256Context->A), (unsigned char*)a_pPsha256Context->A, (unsigned int*)OpcUa_Null);

    OpcUa_ReturnErrorIfNull(a_pHash, OpcUa_Bad);

OpcUa_ReturnStatusCode;

OpcUa_BeginErrorHandling;
OpcUa_FinishErrorHandling;
}
开发者ID:linearregression,项目名称:UA-AnsiC,代码行数:24,代码来源:opcua_p_openssl_random.c


示例20: encrypt_and_authentucate_secrets

int encrypt_and_authentucate_secrets(CPOR_key *key, unsigned char *input, size_t input_len, unsigned char *ciphertext, size_t *ciphertext_len, unsigned char *authenticator, size_t *authenticator_len) {

    EVP_CIPHER_CTX ctx;
    EVP_CIPHER *cipher = NULL;
    int len;

    if(!key || !key->k_enc || !key->k_mac || !input || !input_len || !ciphertext || !ciphertext_len || !authenticator || !authenticator_len) return 0;

    OpenSSL_add_all_algorithms();

    EVP_CIPHER_CTX_init(&ctx);
    switch(key->k_enc_size) {
    case 16:
        cipher = (EVP_CIPHER *)EVP_aes_128_cbc();
        break;
    case 24:
        cipher = (EVP_CIPHER *)EVP_aes_192_cbc();
        break;
    case 32:
        cipher = (EVP_CIPHER *)EVP_aes_256_cbc();
        break;
    default:
        return 0;
    }
    //TODO: Fix the NULL IV
    if(!EVP_EncryptInit(&ctx, cipher, key->k_enc, NULL)) goto cleanup;

    *ciphertext_len = 0;

    if(!EVP_EncryptUpdate(&ctx, ciphertext, (int *)ciphertext_len, input, input_len)) goto cleanup;
    EVP_EncryptFinal(&ctx, ciphertext + *ciphertext_len, &len);

    *ciphertext_len += len;

    *authenticator_len = 0;
    /* Do the HMAC-SHA1 */
    if(!HMAC(EVP_sha1(), key->k_mac, key->k_mac_size, ciphertext, *ciphertext_len,
             authenticator, (unsigned int *)authenticator_len)) goto cleanup;

    EVP_CIPHER_CTX_cleanup(&ctx);

    return 1;

cleanup:
    *ciphertext_len = 0;
    *authenticator_len = 0;

    return 0;

}
开发者ID:nypgit,项目名称:compact-proofs-of-retrievability,代码行数:50,代码来源:cpor-misc.c



注:本文中的HMAC函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ HMAC_CTX_cleanup函数代码示例发布时间:2022-05-30
下一篇:
C++ HLock函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap