Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
/* Compute MD5 hash on in and store the hex string result in out.
*/
void
md5_hex(char *out, size_t size_out, unsigned char *in, size_t size_in)
{
    uint8_t      md[MD5_DIGEST_LEN];

    md5(md, in, size_in);
    digest_to_hex(out, size_out, md, MD5_DIGEST_LEN);
}
Exemplo n.º 3
0
/* Compute SHA512 hash on in and store the hex string result in out.
*/
void
sha512_hex(char *out, size_t size_out, unsigned char *in, size_t size_in)
{
    uint8_t       md[SHA512_DIGEST_LEN];

    sha512(md, in, size_in);
    digest_to_hex(out, size_out, md, SHA512_DIGEST_LEN);
}
Exemplo n.º 4
0
int main(int argc, char** argv)
{
    int k;
    SHA1_CTX context;
    uint8_t digest[20];
    char output[80];

    fprintf(stdout, "verifying SHA-1 implementation... ");
    
    for (k = 0; k < 2; k++){ 
        crypto_SHA1_Init(&context);
        crypto_SHA1_Update(&context, (uint8_t*)test_data[k],
                           strlen(test_data[k]));
        crypto_SHA1_Final(&context, digest);
        digest_to_hex(digest, output);

        if (strcmp(output, test_results[k])) {
            fprintf(stdout, "FAIL\n");
            fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[k]);
            fprintf(stderr,"\t%s returned\n", output);
            fprintf(stderr,"\t%s is correct\n", test_results[k]);
            return (1);
        }    
    }
    /* million 'a' vector we feed separately */
    crypto_SHA1_Init(&context);
    for (k = 0; k < 1000000; k++)
        crypto_SHA1_Update(&context, (uint8_t*)"a", 1);
    crypto_SHA1_Final(&context, digest);
    digest_to_hex(digest, output);
    if (strcmp(output, test_results[2])) {
        fprintf(stdout, "FAIL\n");
        fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[2]);
        fprintf(stderr,"\t%s returned\n", output);
        fprintf(stderr,"\t%s is correct\n", test_results[2]);
        return (1);
    }

    /* success */
    fprintf(stdout, "ok\n");
    return(0);
}
Exemplo n.º 5
0
Arquivo: githash.c Projeto: jpmens/dv
int githash_file(char *filename, char *digest_out)
{
	int             fd, n;
	unsigned char   buf[BLEN], header[128];
	unsigned char   digest[LDNS_SHA1_DIGEST_LENGTH];
	ldns_sha1_ctx	sha;
	struct stat     sb;


	if (stat(filename, &sb) != 0) {
		return (-1);
	}

	if ((fd = open(filename, O_RDONLY)) == -1) {
		return (-1);
	}

	/*
	 * Python: 
	 *	def githash(data):
	 *		s = sha1()
	 *		s.update("blob %u\0" % len(data))
	 *		s.update(data)
	 *		return s.hexdigest()
	 */

	memset(header, 0, sizeof(header));		/* Ensure contains trailing '\0' */
	ldns_sha1_init (&sha);
	sprintf((char *)header, "blob %lu", sb.st_size);
	ldns_sha1_update (&sha, (uint8_t *) header, strlen((char *)header) + 1);

	while ((n = read(fd, buf, sizeof(buf))) > 0) {
		ldns_sha1_update (&sha, (uint8_t *) buf, n);
	}
	ldns_sha1_final ((uint8_t *) digest, &sha);

	close(fd);

	digest_to_hex(digest, digest_out);

	return (0);
}