172 lines · cpp
1// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s2 3#include <assert.h>4#include <sha1.h>5#include <stdio.h>6#include <stdlib.h>7#include <string.h>8 9void test1() {10 SHA1_CTX ctx;11 uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };12 uint8_t digest[SHA1_DIGEST_LENGTH];13 14 SHA1Init(&ctx);15 SHA1Update(&ctx, entropy, __arraycount(entropy));16 SHA1Final(digest, &ctx);17 18 printf("test1: '");19 for (size_t i = 0; i < __arraycount(digest); i++)20 printf("%02x", digest[i]);21 printf("'\n");22}23 24void local_SHA1Update(SHA1_CTX *context, const uint8_t *data, unsigned int len)25{26 unsigned int a, b;27 28 b = context->count[0];29 context->count[0] += len << 3;30 if (context->count[0] < b)31 context->count[1] += (len >> 29) + 1;32 b = (b >> 3) & 63;33 if ((b + len) > 63) {34 memcpy(&context->buffer[b], data, (a = 64 - b));35 SHA1Transform(context->state, context->buffer);36 for ( ; a + 63 < len; a += 64)37 SHA1Transform(context->state, &data[a]);38 b = 0;39 } else {40 a = 0;41 }42 memcpy(&context->buffer[b], &data[a], len - a);43}44 45void test2() {46 SHA1_CTX ctx;47 uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };48 uint8_t digest[SHA1_DIGEST_LENGTH];49 50 SHA1Init(&ctx);51 local_SHA1Update(&ctx, entropy, __arraycount(entropy));52 SHA1Final(digest, &ctx);53 54 printf("test2: '");55 for (size_t i = 0; i < __arraycount(digest); i++)56 printf("%02x", digest[i]);57 printf("'\n");58}59 60void test3() {61 SHA1_CTX ctx;62 uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };63 char digest[SHA1_DIGEST_STRING_LENGTH];64 65 SHA1Init(&ctx);66 SHA1Update(&ctx, entropy, __arraycount(entropy));67 char *p = SHA1End(&ctx, digest);68 assert(p == digest);69 70 printf("test3: '%s'\n", digest);71}72 73void test4() {74 SHA1_CTX ctx;75 uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };76 77 SHA1Init(&ctx);78 SHA1Update(&ctx, entropy, __arraycount(entropy));79 char *p = SHA1End(&ctx, NULL);80 assert(strlen(p) == SHA1_DIGEST_STRING_LENGTH - 1);81 82 printf("test4: '%s'\n", p);83 84 free(p);85}86 87void test5() {88 char digest[SHA1_DIGEST_STRING_LENGTH];89 90 char *p = SHA1File("/etc/fstab", digest);91 assert(p == digest);92 93 printf("test5: '%s'\n", p);94}95 96void test6() {97 char *p = SHA1File("/etc/fstab", NULL);98 assert(strlen(p) == SHA1_DIGEST_STRING_LENGTH - 1);99 100 printf("test6: '%s'\n", p);101 102 free(p);103}104 105void test7() {106 char digest[SHA1_DIGEST_STRING_LENGTH];107 108 char *p = SHA1FileChunk("/etc/fstab", digest, 10, 20);109 assert(p == digest);110 111 printf("test7: '%s'\n", p);112}113 114void test8() {115 char *p = SHA1FileChunk("/etc/fstab", NULL, 10, 20);116 assert(strlen(p) == SHA1_DIGEST_STRING_LENGTH - 1);117 118 printf("test8: '%s'\n", p);119 120 free(p);121}122 123void test9() {124 uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };125 char digest[SHA1_DIGEST_STRING_LENGTH];126 127 char *p = SHA1Data(entropy, __arraycount(entropy), digest);128 assert(p == digest);129 130 printf("test9: '%s'\n", p);131}132 133void test10() {134 uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };135 136 char *p = SHA1Data(entropy, __arraycount(entropy), NULL);137 assert(strlen(p) == SHA1_DIGEST_STRING_LENGTH - 1);138 139 printf("test10: '%s'\n", p);140 141 free(p);142}143 144int main(void) {145 printf("SHA1\n");146 147 test1();148 test2();149 test3();150 test4();151 test5();152 test6();153 test7();154 test8();155 test9();156 test10();157 158 // CHECK: SHA1159 // CHECK: test1: '57d1b759bf3d1811135748cb0328c73b51fa6f57'160 // CHECK: test2: '57d1b759bf3d1811135748cb0328c73b51fa6f57'161 // CHECK: test3: '57d1b759bf3d1811135748cb0328c73b51fa6f57'162 // CHECK: test4: '57d1b759bf3d1811135748cb0328c73b51fa6f57'163 // CHECK: test5: '{{.*}}'164 // CHECK: test6: '{{.*}}'165 // CHECK: test7: '{{.*}}'166 // CHECK: test8: '{{.*}}'167 // CHECK: test9: '57d1b759bf3d1811135748cb0328c73b51fa6f57'168 // CHECK: test10: '57d1b759bf3d1811135748cb0328c73b51fa6f57'169 170 return 0;171}172