hash_test.c revision f1b68725503ff3e46001eee5a1751e29a43a09d1
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff/*
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * Copyright (C) 2000, 2001 Internet Software Consortium.
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff *
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * Permission to use, copy, modify, and distribute this software for any
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * purpose with or without fee is hereby granted, provided that the above
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * copyright notice and this permission notice appear in all copies.
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff *
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff */
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff/* $Id: hash_test.c,v 1.9 2001/11/27 00:55:42 gson Exp $ */
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff#include <config.h>
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff#include <stdio.h>
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff#include <string.h>
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff#include <isc/hmacmd5.h>
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff#include <isc/md5.h>
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff#include <isc/sha1.h>
f9df80f4348ef68043903efa08299480324f4823Michael Graff#include <isc/util.h>
f9df80f4348ef68043903efa08299480324f4823Michael Graff#include <isc/string.h>
fccf7905e8a06067d49ec00c53d4d57a38a71e52Michael Graff
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graffstatic void
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graffprint_digest(char *s, const char *hash, unsigned char *d,
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff unsigned int words)
f9df80f4348ef68043903efa08299480324f4823Michael Graff{
f9df80f4348ef68043903efa08299480324f4823Michael Graff unsigned int i, j;
f9df80f4348ef68043903efa08299480324f4823Michael Graff
f9df80f4348ef68043903efa08299480324f4823Michael Graff printf("hash (%s) %s:\n\t", hash, s);
f9df80f4348ef68043903efa08299480324f4823Michael Graff for (i = 0; i < words; i++) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington printf(" ");
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff for (j = 0; j < 4; j++)
75ec9bc9c7b4f2485647414330122e7b8e188097Andreas Gustafsson printf("%02x", d[i * 4 + j]);
ac77fece9a62537a9e0e5852498ebeda7b2978c3Bob Halley }
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff printf("\n");
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff}
ac77fece9a62537a9e0e5852498ebeda7b2978c3Bob Halley
ac77fece9a62537a9e0e5852498ebeda7b2978c3Bob Halleyint
ac77fece9a62537a9e0e5852498ebeda7b2978c3Bob Halleymain(int argc, char **argv) {
ac77fece9a62537a9e0e5852498ebeda7b2978c3Bob Halley isc_sha1_t sha1;
f9df80f4348ef68043903efa08299480324f4823Michael Graff isc_md5_t md5;
f9df80f4348ef68043903efa08299480324f4823Michael Graff isc_hmacmd5_t hmacmd5;
f9df80f4348ef68043903efa08299480324f4823Michael Graff unsigned char digest[20];
f9df80f4348ef68043903efa08299480324f4823Michael Graff unsigned char buffer[1024];
f9df80f4348ef68043903efa08299480324f4823Michael Graff const unsigned char *s;
f9df80f4348ef68043903efa08299480324f4823Michael Graff unsigned char key[20];
f9df80f4348ef68043903efa08299480324f4823Michael Graff
f9df80f4348ef68043903efa08299480324f4823Michael Graff UNUSED(argc);
f9df80f4348ef68043903efa08299480324f4823Michael Graff UNUSED(argv);
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff
f9df80f4348ef68043903efa08299480324f4823Michael Graff s = "abc";
e223094b2248afa2697c531f75e6f84855638becMichael Graff isc_sha1_init(&sha1);
b02262cbcd550c63f85df76edc6fff556ea5e95dMichael Graff strcpy(buffer, s);
b02262cbcd550c63f85df76edc6fff556ea5e95dMichael Graff isc_sha1_update(&sha1, buffer, strlen(s));
b02262cbcd550c63f85df76edc6fff556ea5e95dMichael Graff isc_sha1_final(&sha1, digest);
b02262cbcd550c63f85df76edc6fff556ea5e95dMichael Graff print_digest(buffer, "sha1", digest, 5);
f9df80f4348ef68043903efa08299480324f4823Michael Graff
f9df80f4348ef68043903efa08299480324f4823Michael Graff s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
f9df80f4348ef68043903efa08299480324f4823Michael Graff isc_sha1_init(&sha1);
f9df80f4348ef68043903efa08299480324f4823Michael Graff strcpy(buffer, s);
f9df80f4348ef68043903efa08299480324f4823Michael Graff isc_sha1_update(&sha1, buffer, strlen(s));
f9df80f4348ef68043903efa08299480324f4823Michael Graff isc_sha1_final(&sha1, digest);
f9df80f4348ef68043903efa08299480324f4823Michael Graff print_digest(buffer, "sha1", digest, 5);
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff
f9df80f4348ef68043903efa08299480324f4823Michael Graff s = "abc";
f9df80f4348ef68043903efa08299480324f4823Michael Graff isc_md5_init(&md5);
f9df80f4348ef68043903efa08299480324f4823Michael Graff strcpy(buffer, s);
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff isc_md5_update(&md5, buffer, strlen(s));
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff isc_md5_final(&md5, digest);
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff print_digest(buffer, "md5", digest, 4);
fccf7905e8a06067d49ec00c53d4d57a38a71e52Michael Graff
f9df80f4348ef68043903efa08299480324f4823Michael Graff /*
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff * The 3 HMAC-MD5 examples from RFC 2104
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff */
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff s = "Hi There";
f9df80f4348ef68043903efa08299480324f4823Michael Graff memset(key, 0x0b, 16);
f9df80f4348ef68043903efa08299480324f4823Michael Graff isc_hmacmd5_init(&hmacmd5, key, 16);
f9df80f4348ef68043903efa08299480324f4823Michael Graff strcpy(buffer, s);
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff isc_hmacmd5_sign(&hmacmd5, digest);
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff print_digest(buffer, "hmacmd5", digest, 4);
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff s = "what do ya want for nothing?";
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff strcpy(key, "Jefe");
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff isc_hmacmd5_init(&hmacmd5, key, 4);
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff strcpy(buffer, s);
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff isc_hmacmd5_sign(&hmacmd5, digest);
f9df80f4348ef68043903efa08299480324f4823Michael Graff print_digest(buffer, "hmacmd5", digest, 4);
f9df80f4348ef68043903efa08299480324f4823Michael Graff
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff s = "\335\335\335\335\335\335\335\335\335\335"
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff "\335\335\335\335\335\335\335\335\335\335"
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff "\335\335\335\335\335\335\335\335\335\335"
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff "\335\335\335\335\335\335\335\335\335\335"
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff "\335\335\335\335\335\335\335\335\335\335";
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff memset(key, 0xaa, 16);
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff isc_hmacmd5_init(&hmacmd5, key, 16);
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff strcpy(buffer, s);
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff isc_hmacmd5_sign(&hmacmd5, digest);
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff print_digest(buffer, "hmacmd5", digest, 4);
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff return (0);
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff}
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff