当前位置: 首页>>代码示例>>C++>>正文


C++ sha1_init函数代码示例

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


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

示例1: hmac_sha1_init

void hmac_sha1_init(hmac_sha1_ctx_t *s, const void* key, uint16_t keylength_b){
	uint8_t buffer[SHA1_BLOCK_BYTES];
	uint8_t i;
	
	memset(buffer, 0, SHA1_BLOCK_BYTES);
	if (keylength_b > SHA1_BLOCK_BITS){
		sha1((void*)buffer, key, keylength_b);
	} else {
		memcpy(buffer, key, (keylength_b+7)/8);
	}
	
	for (i=0; i<SHA1_BLOCK_BYTES; ++i){
		buffer[i] ^= IPAD;
	}
	sha1_init(&(s->a));
	sha1_nextBlock(&(s->a), buffer);
	
	for (i=0; i<SHA1_BLOCK_BYTES; ++i){
		buffer[i] ^= IPAD^OPAD;
	}
	sha1_init(&(s->b));
	sha1_nextBlock(&(s->b), buffer);
	
	
#if defined SECURE_WIPE_BUFFER
	memset(buffer, 0, SHA1_BLOCK_BYTES);
#endif
}
开发者ID:fakedrake,项目名称:cryptostick-truecrypt,代码行数:28,代码来源:hmac-sha1.c

示例2: hmac_sha1_init

/* Begin SHA1 HMAC functions
*/
static void
hmac_sha1_init(hmac_sha1_ctx *ctx, const char *key, const int key_len)
{
    unsigned char  final_key[MAX_DIGEST_BLOCK_LEN] = {0};
    unsigned char  init_key[MAX_DIGEST_BLOCK_LEN]  = {0};
    int            final_len = key_len;

    if(key_len > MAX_DIGEST_BLOCK_LEN)
        final_len = MAX_DIGEST_BLOCK_LEN;

    memcpy(init_key, key, final_len);

    if(SHA1_BLOCK_LEN < key_len)
    {
        /* Calculate the digest of the key
        */
        sha1(final_key, init_key, final_len);
    }
    else
    {
        memcpy(final_key, init_key, key_len);
    }

    pad_init(ctx->block_inner_pad, ctx->block_outer_pad, final_key, final_len);

    sha1_init(&ctx->ctx_inside);
    sha1_update(&ctx->ctx_inside, ctx->block_inner_pad, SHA1_BLOCK_LEN);

    sha1_init(&ctx->ctx_outside);
    sha1_update(&ctx->ctx_outside, ctx->block_outer_pad, SHA1_BLOCK_LEN);

    return;
}
开发者ID:PHPDOTSQL,项目名称:fwknop,代码行数:35,代码来源:hmac.c

示例3: sha1_hmac_init

void
sha1_hmac_init(struct sha1_hmac_context *ctx, const byte *key, uint keylen)
{
  byte keybuf[SHA1_BLOCK_SIZE], buf[SHA1_BLOCK_SIZE];

  /* Hash the key if necessary */
  if (keylen <= SHA1_BLOCK_SIZE)
  {
    memcpy(keybuf, key, keylen);
    memset(keybuf + keylen, 0, SHA1_BLOCK_SIZE - keylen);
  }
  else
  {
    sha1_hash_buffer(keybuf, key, keylen);
    memset(keybuf + SHA1_SIZE, 0, SHA1_BLOCK_SIZE - SHA1_SIZE);
  }

  /* Initialize the inner digest */
  sha1_init(&ctx->ictx);
  int i;
  for (i = 0; i < SHA1_BLOCK_SIZE; i++)
    buf[i] = keybuf[i] ^ 0x36;
  sha1_update(&ctx->ictx, buf, SHA1_BLOCK_SIZE);

  /* Initialize the outer digest */
  sha1_init(&ctx->octx);
  for (i = 0; i < SHA1_BLOCK_SIZE; i++)
    buf[i] = keybuf[i] ^ 0x5c;
  sha1_update(&ctx->octx, buf, SHA1_BLOCK_SIZE);
}
开发者ID:ColinBS,项目名称:bird,代码行数:30,代码来源:sha1.c

示例4: test_compress_file

static void test_compress_file(const char *in_path, const char *out_path)
{
	const struct compression_handler *handler;
	struct istream *input, *file_input;
	struct ostream *output, *file_output;
	int fd_in, fd_out;
	struct sha1_ctxt sha1;
	unsigned char output_sha1[SHA1_RESULTLEN], input_sha1[SHA1_RESULTLEN];
	const unsigned char *data;
	size_t size;
	ssize_t ret;

	handler = compression_lookup_handler_from_ext(out_path);
	if (handler == NULL)
		i_fatal("Can't detect compression algorithm from path %s", out_path);
	if (handler->create_ostream == NULL)
		i_fatal("Support not compiled in for %s", handler->name);

	/* write the compressed output file */
	fd_in = open(in_path, O_RDONLY);
	if (fd_in == -1)
		i_fatal("open(%s) failed: %m", in_path);
	fd_out = open(out_path, O_TRUNC | O_CREAT | O_RDWR, 0600);
	if (fd_out == -1)
		i_fatal("creat(%s) failed: %m", out_path);

	sha1_init(&sha1);
	file_output = o_stream_create_fd_file(fd_out, 0, FALSE);
	output = handler->create_ostream(file_output, 1);
	input = i_stream_create_fd_autoclose(&fd_in, IO_BLOCK_SIZE);
	while (i_stream_read_data(input, &data, &size, 0) > 0) {
		sha1_loop(&sha1, data, size);
		o_stream_nsend(output, data, size);
		i_stream_skip(input, size);
	}
	if (o_stream_nfinish(output) < 0) {
		i_fatal("write(%s) failed: %s",
			out_path, o_stream_get_error(output));
	}
	i_stream_destroy(&input);
	o_stream_destroy(&output);
	o_stream_destroy(&file_output);
	sha1_result(&sha1, output_sha1);

	/* verify that we can read the compressed file */
	sha1_init(&sha1);
	file_input = i_stream_create_fd(fd_out, IO_BLOCK_SIZE, FALSE);
	input = handler->create_istream(file_input, FALSE);
	while ((ret = i_stream_read_data(input, &data, &size, 0)) > 0) {
		sha1_loop(&sha1, data, size);
		i_stream_skip(input, size);
	}
	i_stream_destroy(&input);
	i_stream_destroy(&file_input);
	sha1_result(&sha1, input_sha1);

	if (memcmp(input_sha1, output_sha1, sizeof(input_sha1)) != 0)
		i_fatal("Decompression couldn't get the original input");
	i_close_fd(&fd_out);
}
开发者ID:jwm,项目名称:dovecot-notmuch,代码行数:60,代码来源:test-compression.c

示例5: hmac_sha1

/*
 * keylength in bits!
 * message length in bits!
 */
void hmac_sha1(void* dest, const void* key, uint16_t keylength_b, const void* msg, uint32_t msglength_b){ /* a one-shot*/
	sha1_ctx_t s;
	uint8_t i;
	uint8_t buffer[SHA1_BLOCK_BYTES];
	
	memset(buffer, 0, SHA1_BLOCK_BYTES);
	
	/* if key is larger than a block we have to hash it*/
	if (keylength_b > SHA1_BLOCK_BITS){
		sha1((void*)buffer, key, keylength_b);
	} else {
		memcpy(buffer, key, (keylength_b+7)/8);
	}
	
	for (i=0; i<SHA1_BLOCK_BYTES; ++i){
		buffer[i] ^= IPAD;
	}
	sha1_init(&s);
	sha1_nextBlock(&s, buffer);
	while (msglength_b >= SHA1_BLOCK_BITS){
		sha1_nextBlock(&s, msg);
		msg = (uint8_t*)msg + SHA1_BLOCK_BYTES;
		msglength_b -=  SHA1_BLOCK_BITS;
	}
	sha1_lastBlock(&s, msg, msglength_b);
	/* since buffer still contains key xor ipad we can do ... */
	for (i=0; i<SHA1_BLOCK_BYTES; ++i){
		buffer[i] ^= IPAD ^ OPAD;
	}
	sha1_ctx2hash(dest, &s); /* save inner hash temporary to dest */
	sha1_init(&s);
	sha1_nextBlock(&s, buffer);
	sha1_lastBlock(&s, dest, SHA1_HASH_BITS);
	sha1_ctx2hash(dest, &s);
}
开发者ID:fakedrake,项目名称:cryptostick-truecrypt,代码行数:39,代码来源:hmac-sha1.c

示例6: hmac_sha1_init

void
hmac_sha1_init(hmac_sha1_ctx *ctx, const void *key, size_t keylen)
{
	uint8_t keybuf[SHA1_BLOCK_LEN], pad[SHA1_BLOCK_LEN];

	/* prepare key */
	memset(keybuf, 0, sizeof keybuf);
        if (keylen > sizeof keybuf)
                sha1_complete(key, keylen, keybuf);
        else
                memcpy(keybuf, key, keylen);

	/* input pad */
	for (unsigned int i = 0; i < sizeof pad; ++i)
		pad[i] = 0x36 ^ keybuf[i];
	sha1_init(&ctx->ictx);
	sha1_update(&ctx->ictx, pad, sizeof pad);

	/* output pad */
	for (unsigned int i = 0; i < sizeof pad; ++i)
		pad[i] = 0x5c ^ keybuf[i];
	sha1_init(&ctx->octx);
	sha1_update(&ctx->octx, pad, sizeof pad);

	/* hide the evidence */
	memset(keybuf, 0, sizeof keybuf);
	memset(pad, 0, sizeof pad);
}
开发者ID:cryb-to,项目名称:cryb-to,代码行数:28,代码来源:cryb_hmac_sha1.c

示例7: hmac_sha1

void hmac_sha1(const uint8_t *key, int keyLength,
               const uint8_t *data, int dataLength,
               uint8_t *result, int resultLength) {
  SHA1_INFO ctx;
  uint8_t hashed_key[SHA1_DIGEST_LENGTH];
  if (keyLength > 64) {
    // The key can be no bigger than 64 bytes. If it is, we'll hash it down to
    // 20 bytes.
    sha1_init(&ctx);
    sha1_update(&ctx, key, keyLength);
    sha1_final(&ctx, hashed_key);
    key = hashed_key;
    keyLength = SHA1_DIGEST_LENGTH;
  }

  // The key for the inner digest is derived from our key, by padding the key
  // the full length of 64 bytes, and then XOR'ing each byte with 0x36.
  uint8_t tmp_key[64];
  for (int i = 0; i < keyLength; ++i) {
    tmp_key[i] = key[i] ^ 0x36;
  }
  if (keyLength < 64) {
    memset(tmp_key + keyLength, 0x36, 64 - keyLength);
  }

  // Compute inner digest
  sha1_init(&ctx);
  sha1_update(&ctx, tmp_key, 64);
  sha1_update(&ctx, data, dataLength);
  uint8_t sha[SHA1_DIGEST_LENGTH];
  sha1_final(&ctx, sha);

  // The key for the outer digest is derived from our key, by padding the key
  // the full length of 64 bytes, and then XOR'ing each byte with 0x5C.
  for (int i = 0; i < keyLength; ++i) {
    tmp_key[i] = key[i] ^ 0x5C;
  }
  memset(tmp_key + keyLength, 0x5C, 64 - keyLength);

  // Compute outer digest
  sha1_init(&ctx);
  sha1_update(&ctx, tmp_key, 64);
  sha1_update(&ctx, sha, SHA1_DIGEST_LENGTH);
  sha1_final(&ctx, sha);

  // Copy result to output buffer and truncate or pad as necessary
  memset(result, 0, resultLength);
  if (resultLength > SHA1_DIGEST_LENGTH) {
    resultLength = SHA1_DIGEST_LENGTH;
  }
  memcpy(result, sha, resultLength);

  // Zero out all internal data structures
  memset(hashed_key, 0, sizeof(hashed_key));
  memset(sha, 0, sizeof(sha));
  memset(tmp_key, 0, sizeof(tmp_key));
}
开发者ID:42p,项目名称:google-authenticator,代码行数:57,代码来源:hmac.c

示例8: main

int
main(int argc, char *argv[])
{
	unsigned int i;
	struct sha1_context ctx;
	char digest[SHA1_DIGEST_LENGTH];
	char output[2*SHA1_DIGEST_LENGTH + 5];

	/* silence gcc -Wextra */
	(void)argc;
	(void)argv;

	printf("Verifying SHA-1 implementation... ");
	fflush(stdout);

	for (i = 0; i < sizeof(test_data) / sizeof(test_data[0]); i++) {
		sha1_init(&ctx);
		sha1_update(&ctx, test_data[i], strlen(test_data[i]));
		sha1_final(&ctx, digest);
		digest_to_hex(digest, output);

		if (strcmp(output, test_results[i])) {
			fprintf(stdout, "FAIL\n");
			fprintf(stderr, "* hash of \"%s\" incorrect:\n"
			                "\t%s returned\n"
			                "\t%s is correct\n",
			                test_data[i], output, test_results[i]);
			return EXIT_FAILURE;
		}
	}

	/* the million 'a' vector we feed separately */
	sha1_init(&ctx);
	for (i = 0; i < 1000000; i++)
		sha1_update(&ctx, "a", 1);
	sha1_final(&ctx, digest);
	digest_to_hex(digest, output);

	if (strcmp(output, "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F")) {
		fprintf(stdout, "FAIL\n");
		fprintf(stderr, "* hash of a million a's is incorrect:\n"
		                "\t%s returned\n"
		                "\t34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"
		                " is correct\n", output);
		return 1;
	}

	printf("OK\n");
	fflush(stdout);

	return EXIT_SUCCESS;
}
开发者ID:timgarbos,项目名称:doorduino,代码行数:52,代码来源:sha1.c

示例9: hmac_init

err_status_t
hmac_init(hmac_ctx_t *state, const octet_t *key, int key_len) {
  int i;

  /*
   * check key length - note that we don't support keys larger
   * than 20 bytes yet
   */
  if (key_len > 20)              
    return err_status_bad_param;
  
  /*
   * set values of ipad and opad in the context by exoring the key
   * into the appropriate constant values
   */
  for (i=0; i < key_len; i++) {    
    state->ipad[i] = key[i] ^ 0x36;
    state->opad[i] = key[i] ^ 0x5c;
  }  
  /* set the rest of ipad, opad to constant values */
  for (   ; i < 64; i++) {    
    ((octet_t *)state->ipad)[i] = 0x36;
    ((octet_t *)state->opad)[i] = 0x5c;
  }  

  debug_print(mod_hmac, "ipad: %s", octet_string_hex_string(state->ipad, 64));
  
  /* initialize sha1 context */
  sha1_init(&state->ctx);

  /* hash ipad ^ key */
  sha1_update(&state->ctx, (octet_t *)state->ipad, 64);

  return err_status_ok;
}
开发者ID:gabrieldelsaint,项目名称:UIM,代码行数:35,代码来源:hmac.c

示例10: sha1_test_case_validate

err_status_t
sha1_test_case_validate(const hash_test_case_t *test_case) {
  sha1_ctx_t ctx;
  uint32_t hash_value[5];

  if (test_case == NULL)
    return err_status_bad_param;

  if (test_case->hash_len != 20)
    return err_status_bad_param;
  if (test_case->data_len > MAX_HASH_DATA_LEN)
    return err_status_bad_param;

  sha1_init(&ctx);
  sha1_update(&ctx, test_case->data, test_case->data_len);
  sha1_final(&ctx, hash_value);
  if (0 == memcmp(test_case->hash, hash_value, 20)) {
#if VERBOSE
    printf("PASSED: reference value: %s\n", 
	   octet_string_hex_string((const uint8_t *)test_case->hash, 20));
    printf("PASSED: computed value:  %s\n", 
	   octet_string_hex_string((const uint8_t *)hash_value, 20));   
#endif 
    return err_status_ok;
  }

  printf("reference value: %s\n", 
	 octet_string_hex_string((const uint8_t *)test_case->hash, 20));
  printf("computed value:  %s\n", 
	 octet_string_hex_string((const uint8_t *)hash_value, 20));

  return err_status_algo_fail;
  
}
开发者ID:ChristyAJones,项目名称:libsrtp,代码行数:34,代码来源:sha1_driver.c

示例11: compute_hash

		virtual udx_hash		compute_hash(void* addrin, u32 addrinlen)
		{
			udx_hash hash;
			sha1_init(&m_ctx);
			sha1_update(&m_ctx, (const u8*)addrin, addrinlen);
			sha1_final(&m_ctx, (u8*)hash.m_hash);
		}
开发者ID:jurgen-kluft,项目名称:xp2p,代码行数:7,代码来源:x_udx-address.cpp

示例12: test_one

static void
test_one(const struct test_vector *vec)
{
    uint8_t md[SHA1_DIGEST_SIZE];
    int i;

    /* All at once. */
    sha1_bytes(vec->data, vec->size, md);
    assert(!memcmp(md, vec->output, SHA1_DIGEST_SIZE));

    /* In two pieces. */
    for (i = 0; i < 20; i++) {
        int n0 = vec->size ? random_range(vec->size) : 0;
        int n1 = vec->size - n0;
        struct sha1_ctx sha1;

        sha1_init(&sha1);
        sha1_update(&sha1, vec->data, n0);
        sha1_update(&sha1, vec->data + n0, n1);
        sha1_final(&sha1, md);
        assert(!memcmp(md, vec->output, SHA1_DIGEST_SIZE));
    }

    putchar('.');
    fflush(stdout);
}
开发者ID:AlickHill,项目名称:Lantern,代码行数:26,代码来源:test-sha1.c

示例13: sha1_test

/**
  Self-test the hash
  @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
*/
int sha1_test(void) {
    static const struct {
        char* msg;
        unsigned char hash[20];
    } tests[] = {{"abc",
                  {0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78,
                   0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d}},
                 {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
                  {0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE, 0x4A, 0xA1, 0xF9,
                   0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1}}};

    int i;
    unsigned char tmp[20];
    sha1_state state;

    for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
        sha1_init(&state);
        sha1_process(&state, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
        sha1_done(&state, tmp);
        if (memcpy(tmp, tests[i].hash, 20) != 0) {
            return CRYPT_FAIL_TESTVECTOR;
        }
    }
    return CRYPT_OK;
}
开发者ID:NSGod,项目名称:chmlib,代码行数:29,代码来源:sha1.c

示例14: parse_body

static struct ovsdb_error *
parse_body(struct ovsdb_log *file, off_t offset, unsigned long int length,
           uint8_t sha1[SHA1_DIGEST_SIZE], struct json **jsonp)
{
    struct json_parser *parser;
    struct sha1_ctx ctx;

    sha1_init(&ctx);
    parser = json_parser_create(JSPF_TRAILER);

    while (length > 0) {
        char input[BUFSIZ];
        int chunk;

        chunk = MIN(length, sizeof input);
        if (fread(input, 1, chunk, file->stream) != chunk) {
            json_parser_abort(parser);
            return ovsdb_io_error(ferror(file->stream) ? errno : EOF,
                                  "%s: error reading %lu bytes "
                                  "starting at offset %lld", file->name,
                                  length, (long long int) offset);
        }
        sha1_update(&ctx, input, chunk);
        json_parser_feed(parser, input, chunk);
        length -= chunk;
    }

    sha1_final(&ctx, sha1);
    *jsonp = json_parser_finish(parser);
    return NULL;
}
开发者ID:l8huang,项目名称:ovs,代码行数:31,代码来源:log.c

示例15: ssh_mac_ctx_init

ssh_mac_ctx ssh_mac_ctx_init(enum ssh_mac_e type){
  ssh_mac_ctx ctx = malloc(sizeof(struct ssh_mac_ctx_struct));
  if (ctx == NULL) {
    return NULL;
  }

  ctx->mac_type=type;
  switch(type){
    case SSH_MAC_SHA1:
      ctx->ctx.sha1_ctx = sha1_init();
      return ctx;
    case SSH_MAC_SHA256:
      ctx->ctx.sha256_ctx = sha256_init();
      return ctx;
    case SSH_MAC_SHA384:
      ctx->ctx.sha384_ctx = sha384_init();
      return ctx;
    case SSH_MAC_SHA512:
      ctx->ctx.sha512_ctx = sha512_init();
      return ctx;
    default:
      SAFE_FREE(ctx);
      return NULL;
  }
}
开发者ID:codinn,项目名称:libssh,代码行数:25,代码来源:libcrypto.c


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