DEFINITIONS
This source file includes following functions.
- RMD160_End
- RMD160_File
- RMD160_Data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 #include "rmd160.h"
17
18 #ifndef lint
19
20 #endif
21
22
23
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #if defined(HAVE_UNISTD_H)
30 # include <unistd.h>
31 #endif
32
33 #ifndef _DIAGASSERT
34 #define _DIAGASSERT(cond) assert(cond)
35 #endif
36
37
38 char *
39 RMD160_End(RMD160_CTX *ctx, char *buf)
40 {
41 size_t i;
42 char *p = buf;
43 uint8_t digest[20];
44 static const char hex[]="0123456789abcdef";
45
46 _DIAGASSERT(ctx != NULL);
47
48
49 if (p == NULL && (p = malloc(41)) == NULL)
50 return 0;
51
52 RMD160_Final(digest,ctx);
53 for (i = 0; i < 20; i++) {
54 p[i + i] = hex[(uint32_t)digest[i] >> 4];
55 p[i + i + 1] = hex[digest[i] & 0x0f];
56 }
57 p[i + i] = '\0';
58 return(p);
59 }
60
61 char *
62 RMD160_File(char *filename, char *buf)
63 {
64 uint8_t buffer[BUFSIZ];
65 RMD160_CTX ctx;
66 int fd, num, oerrno;
67
68 _DIAGASSERT(filename != NULL);
69
70
71 RMD160_Init(&ctx);
72
73 if ((fd = open(filename, O_RDONLY)) < 0)
74 return(0);
75
76 while ((num = read(fd, buffer, sizeof(buffer))) > 0)
77 RMD160_Update(&ctx, buffer, (size_t)num);
78
79 oerrno = errno;
80 close(fd);
81 errno = oerrno;
82 return(num < 0 ? 0 : RMD160_End(&ctx, buf));
83 }
84
85 char *
86 RMD160_Data(const uint8_t *data, size_t len, char *buf)
87 {
88 RMD160_CTX ctx;
89
90 _DIAGASSERT(data != NULL);
91
92
93 RMD160_Init(&ctx);
94 RMD160_Update(&ctx, data, len);
95 return(RMD160_End(&ctx, buf));
96 }