File: multihash.c

package info (click to toggle)
md5deep 3.6-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,220 kB
  • ctags: 667
  • sloc: ansic: 7,134; sh: 3,195; makefile: 105
file content (74 lines) | stat: -rw-r--r-- 1,574 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

/* $Id: multihash.c 168 2009-01-10 14:15:08Z jessekornblum $ */

#include "main.h"

void multihash_initialize(state *s)
{
  hashname_t i;

  for (i = 0 ; i < NUM_ALGORITHMS ; ++i)
    {
      if (s->hashes[i]->inuse)
	{	  
	  memset(s->current_file->hash[i],0,s->hashes[i]->byte_length);
	  s->hashes[i]->f_init(s->hashes[i]->hash_context);
	}
    }
}


void multihash_update(state *s, unsigned char *buf, uint64_t len)
{
  hashname_t i;

  // We have to copy the data being hashed from the buffer we were
  // passed into another structure because the SHA-1 update 
  // routine modifies it.
  for (i = 0 ; i < NUM_ALGORITHMS ; ++i)
  {
    if (s->hashes[i]->inuse)
    {
      memcpy(s->buffer,buf,len);
      s->hashes[i]->f_update(s->hashes[i]->hash_context,s->buffer,len);
    }
  }
}


void multihash_finalize(state *s)
{
  hashname_t i;
  uint16_t j, len;
  static char hex[] = "0123456789abcdef";

  char * result;

  for (i = 0 ; i < NUM_ALGORITHMS ; ++i)
    {
      if (s->hashes[i]->inuse)
	{
	  s->hashes[i]->f_finalize(s->hashes[i]->hash_context,
				   s->hashes[i]->hash_sum);
	  
	  len = s->hashes[i]->byte_length / 2;       
	  
	  /* Shorthand to make the code easier to read */
	  result = s->current_file->hash[i];

	  for (j = 0; j < len ; ++j) 
	    {
	      result[2 * j] = hex[(s->hashes[i]->hash_sum[j] >> 4) & 0xf];
	      result[2 * j + 1] = hex[s->hashes[i]->hash_sum[j] & 0xf];
	      
	    }
	  result[s->hashes[i]->byte_length] = 0;
	}
    }

  s->current_file->file_name = s->full_name;
  s->current_file->file_size = s->total_bytes;
}