1 /* mdXhl.c * ----------------------------------------------------------------------------
2 * "THE BEER-WARE LICENSE" (Revision 42):
3 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
4 * can do whatever you want with this stuff. If we meet some day, and you think
5 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
6 * ----------------------------------------------------------------------------
8 * $FreeBSD: src/lib/libmd/mdXhl.c,v 1.19 2006/01/17 15:35:56 phk Exp $
9 * $DragonFly: src/lib/libmd/mdXhl.c,v 1.3 2008/09/11 20:25:34 swildner Exp $
12 * This code has been deprecated, do not put this in libmd or anywhere else please.
13 * The few base system programs that use this code will .PATH it in.
15 * Note that libcrypto/lib[re]ssl provides the standard API that this file extends
16 * for these functions.
19 #include <sys/types.h>
34 SHA1_End(SHA1_CTX
*ctx
, char *buf
)
37 unsigned char digest
[LENGTH
];
38 static const char hex
[]="0123456789abcdef";
41 buf
= malloc(2*LENGTH
+ 1);
44 SHA1_Final(digest
, ctx
);
45 for (i
= 0; i
< LENGTH
; i
++) {
46 buf
[i
+i
] = hex
[digest
[i
] >> 4];
47 buf
[i
+i
+1] = hex
[digest
[i
] & 0x0f];
54 SHA1_File(const char *filename
, char *buf
)
56 return (SHA1_FileChunk(filename
, buf
, 0, 0));
60 SHA1_FileChunk(const char *filename
, char *buf
, off_t ofs
, off_t len
)
62 unsigned char buffer
[8192];
69 f
= open(filename
, O_RDONLY
);
72 if (fstat(f
, &stbuf
) < 0)
74 if (ofs
> stbuf
.st_size
)
76 if ((len
== 0) || (len
> stbuf
.st_size
- ofs
))
77 len
= stbuf
.st_size
- ofs
;
78 if (lseek(f
, ofs
, SEEK_SET
) < 0)
83 if ((size_t)n
> sizeof(buffer
))
84 i
= read(f
, buffer
, sizeof(buffer
));
86 i
= read(f
, buffer
, n
);
89 SHA1_Update(&ctx
, buffer
, i
);
97 return (SHA1_End(&ctx
, buf
));
101 SHA1_Data (const void *data
, unsigned int len
, char *buf
)
106 SHA1_Update(&ctx
,data
,len
);
107 return (SHA1_End(&ctx
, buf
));