2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright 2016 Toomas Soome <tsoome@me.com>
14 * Copyright 2017 OmniTI Computer Consulting, Inc. All rights reserved.
18 * Create sha1 hash for file.
20 * NOTE: This is hardwired for now, so use libmd's SHA1 for simplicity.
26 #include <sys/types.h>
31 #include <cryptoutil.h>
34 #define BUFFERSIZE (64 * 1024)
35 static uint8_t buf
[BUFFERSIZE
];
38 bootadm_digest(const char *filename
, char **result
)
41 char *resultstr
= NULL
;
43 int resultstrlen
, resultlen
, exitcode
;
47 /* Allocate a buffer to store result. */
48 resultlen
= SHA1_DIGEST_LENGTH
;
49 if ((resultbuf
= malloc(resultlen
)) == NULL
) {
50 bam_print(gettext("out of memory\n"));
55 if ((fd
= open(filename
, O_RDONLY
| O_NONBLOCK
)) == -1) {
56 bam_print(gettext("can not open input file %s\n"), filename
);
62 while ((nread
= read(fd
, buf
, sizeof (buf
))) > 0)
63 SHA1Update(&sha1_ctx
, buf
, nread
);
65 bam_print(gettext("error reading file: %s\n"), strerror(errno
));
69 SHA1Final(resultbuf
, &sha1_ctx
);
71 /* Allocate a buffer to store result string */
72 resultstrlen
= 2 * resultlen
+ 1; /* Two hex chars per byte. */
73 if ((resultstr
= malloc(resultstrlen
)) == NULL
) {
74 bam_print(gettext("out of memory\n"));
79 tohexstr(resultbuf
, resultlen
, resultstr
, resultstrlen
);
80 exitcode
= BAM_SUCCESS
;
83 if (exitcode
== BAM_ERROR
) {