1 /* $NetBSD: sha1hl.c,v 1.7 2007/09/21 18:44:37 joerg Exp $ */
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 * ----------------------------------------------------------------------------
12 /* #include "namespace.h" */
21 #ifdef HAVE_SYS_FILE_H
35 #if defined(LIBC_SCCS) && !defined(lint)
36 __RCSID("$NetBSD: sha1hl.c,v 1.7 2007/09/21 18:44:37 joerg Exp $");
37 #endif /* LIBC_SCCS and not lint */
40 #define _DIAGASSERT(cond) assert(cond)
45 SHA1End(SHA1_CTX
*ctx
, char *buf
)
50 static const char hex
[]="0123456789abcdef";
52 _DIAGASSERT(ctx
!= NULL
);
55 if (p
== NULL
&& (p
= malloc(41)) == NULL
)
58 SHA1Final(digest
,ctx
);
59 for (i
= 0; i
< 20; i
++) {
60 p
[i
+ i
] = hex
[((uint32_t)digest
[i
]) >> 4];
61 p
[i
+ i
+ 1] = hex
[digest
[i
] & 0x0f];
68 SHA1File(char *filename
, char *buf
)
70 uint8_t buffer
[BUFSIZ
];
75 _DIAGASSERT(filename
!= NULL
);
76 /* XXX: buf may be NULL ? */
80 if ((fd
= open(filename
,O_RDONLY
)) < 0)
83 while ((num
= read(fd
, buffer
, sizeof(buffer
))) > 0)
84 SHA1Update(&ctx
, buffer
, (size_t)num
);
89 return(num
< 0 ? 0 : SHA1End(&ctx
, buf
));
93 SHA1Data(const uint8_t *data
, size_t len
, char *buf
)
97 _DIAGASSERT(data
!= NULL
);
98 /* XXX: buf may be NULL ? */
101 SHA1Update(&ctx
, data
, len
);
102 return(SHA1End(&ctx
, buf
));