]>
git.cameronkatri.com Git - apple_cmds.git/blob - text_cmds/sort/commoncrypto.c
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD: head/lib/libmd/mdXhl.c 294037 2016-01-14 21:08:23Z jtl $");
13 #include <sys/types.h>
22 #include "commoncrypto.h"
24 #define LENGTH CC_MD5_DIGEST_LENGTH
26 char *MD5FileChunk(const char *, char *, off_t
, off_t
);
29 MD5End(CC_MD5_CTX
*ctx
, char *buf
)
32 unsigned char digest
[LENGTH
];
33 static const char hex
[]="0123456789abcdef";
36 buf
= malloc(2*LENGTH
+ 1);
39 CC_MD5_Final(digest
, ctx
);
40 for (i
= 0; i
< LENGTH
; i
++) {
41 buf
[i
+i
] = hex
[digest
[i
] >> 4];
42 buf
[i
+i
+1] = hex
[digest
[i
] & 0x0f];
49 MD5File(const char *filename
, char *buf
)
51 return (MD5FileChunk(filename
, buf
, 0, 0));
55 MD5FileChunk(const char *filename
, char *buf
, off_t ofs
, off_t len
)
57 unsigned char buffer
[16*1024];
68 fd
= open(filename
, O_RDONLY
);
73 if (lseek(fd
, ofs
, SEEK_SET
) != ofs
||
74 (ofs
== -1 && errno
!= 0)) {
81 while (len
== 0 || remain
> 0) {
82 if (len
== 0 || remain
> (off_t
)sizeof(buffer
))
83 readrv
= read(fd
, buffer
, sizeof(buffer
));
85 readrv
= read(fd
, buffer
, remain
);
88 CC_MD5_Update(&ctx
, buffer
, readrv
);
97 return (MD5End(&ctx
, buf
));