| 1 | $FreeWRT$
|
|---|
| 2 | --- busybox-1.1.3.orig/libbb/hash.c Thu Jan 1 00:00:00 1970
|
|---|
| 3 | +++ busybox-1.1.3/libbb/hash.c Sat Nov 25 19:54:19 2006
|
|---|
| 4 | @@ -0,0 +1,100 @@
|
|---|
| 5 | +/*
|
|---|
| 6 | + * Copyright (C) 2003 Glenn L. McGrath
|
|---|
| 7 | + * Copyright (C) 2003-2004 Erik Andersen
|
|---|
| 8 | + *
|
|---|
| 9 | + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
|---|
| 10 | + */
|
|---|
| 11 | +
|
|---|
| 12 | +#include <fcntl.h>
|
|---|
| 13 | +#include <limits.h>
|
|---|
| 14 | +#include <stdio.h>
|
|---|
| 15 | +#include <stdint.h>
|
|---|
| 16 | +#include <stdlib.h>
|
|---|
| 17 | +#include <string.h>
|
|---|
| 18 | +#include <unistd.h>
|
|---|
| 19 | +
|
|---|
| 20 | +#include "busybox.h"
|
|---|
| 21 | +
|
|---|
| 22 | +unsigned char *hash_bin_to_hex(unsigned char *hash_value, unsigned char hash_length)
|
|---|
| 23 | +{
|
|---|
| 24 | + int x, len, max;
|
|---|
| 25 | + unsigned char *hex_value;
|
|---|
| 26 | +
|
|---|
| 27 | + max = (hash_length * 2) + 2;
|
|---|
| 28 | + hex_value = xmalloc(max);
|
|---|
| 29 | + for (x = len = 0; x < hash_length; x++) {
|
|---|
| 30 | + len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
|
|---|
| 31 | + }
|
|---|
| 32 | + return (hex_value);
|
|---|
| 33 | +}
|
|---|
| 34 | +
|
|---|
| 35 | +int hash_fd(int fd, hash_algo_t hash_algo, uint8_t *hash_value)
|
|---|
| 36 | +{
|
|---|
| 37 | + int count, result = 0;
|
|---|
| 38 | + union _ctx_ {
|
|---|
| 39 | + sha1_ctx_t sha1;
|
|---|
| 40 | + md5_ctx_t md5;
|
|---|
| 41 | + } context;
|
|---|
| 42 | + RESERVE_CONFIG_UBUFFER(in_buf, 4096);
|
|---|
| 43 | + void (*update)(const void*, size_t, void*) = NULL;
|
|---|
| 44 | + void (*final)(void*, void*) = NULL;
|
|---|
| 45 | +
|
|---|
| 46 | + // figure specific hash algorithims
|
|---|
| 47 | + if(hash_algo==HASH_MD5) {
|
|---|
| 48 | + md5_begin(&context.md5);
|
|---|
| 49 | + update = (void (*)(const void*, size_t, void*))md5_hash;
|
|---|
| 50 | + final = (void (*)(void*, void*))md5_end;
|
|---|
| 51 | + } else if(hash_algo==HASH_SHA1) {
|
|---|
| 52 | + sha1_begin(&context.sha1);
|
|---|
| 53 | + update = (void (*)(const void*, size_t, void*))sha1_hash;
|
|---|
| 54 | + final = (void (*)(void*, void*))sha1_end;
|
|---|
| 55 | + }
|
|---|
| 56 | +
|
|---|
| 57 | +
|
|---|
| 58 | + while(0 < (count = read(fd, in_buf, sizeof in_buf))) {
|
|---|
| 59 | + update(in_buf, count, &context);
|
|---|
| 60 | + result += count;
|
|---|
| 61 | + }
|
|---|
| 62 | +
|
|---|
| 63 | + if(count == 0) {
|
|---|
| 64 | + final(hash_value, &context);
|
|---|
| 65 | + }
|
|---|
| 66 | +
|
|---|
| 67 | + RELEASE_CONFIG_BUFFER(in_buf);
|
|---|
| 68 | +
|
|---|
| 69 | + return result;
|
|---|
| 70 | +}
|
|---|
| 71 | +
|
|---|
| 72 | +uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
|
|---|
| 73 | +{
|
|---|
| 74 | + int src_fd, hash_len;
|
|---|
| 75 | + RESERVE_CONFIG_UBUFFER(hash_buf, 20);
|
|---|
| 76 | + uint8_t *hash_value = NULL;
|
|---|
| 77 | +
|
|---|
| 78 | + if(ENABLE_MD5SUM && hash_algo==HASH_MD5) {
|
|---|
| 79 | + hash_len = 16;
|
|---|
| 80 | + } else if(ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
|
|---|
| 81 | + hash_len = 20;
|
|---|
| 82 | + } else {
|
|---|
| 83 | + bb_error_msg_and_die("algotithm not supported");
|
|---|
| 84 | + }
|
|---|
| 85 | +
|
|---|
| 86 | + if(strcmp(filename, "-") == 0) {
|
|---|
| 87 | + src_fd = STDIN_FILENO;
|
|---|
| 88 | + } else if(0 > (src_fd = open(filename, O_RDONLY))) {
|
|---|
| 89 | + bb_perror_msg("%s", filename);
|
|---|
| 90 | + return NULL;
|
|---|
| 91 | + }
|
|---|
| 92 | +
|
|---|
| 93 | + if(hash_fd(src_fd, hash_algo, hash_buf) > 0) {
|
|---|
| 94 | + hash_value = hash_bin_to_hex(hash_buf, hash_len);
|
|---|
| 95 | + }
|
|---|
| 96 | +
|
|---|
| 97 | + if(src_fd != STDIN_FILENO) {
|
|---|
| 98 | + close(src_fd);
|
|---|
| 99 | + }
|
|---|
| 100 | +
|
|---|
| 101 | + RELEASE_CONFIG_BUFFER(hash_buf);
|
|---|
| 102 | +
|
|---|
| 103 | + return hash_value;
|
|---|
| 104 | +}
|
|---|