Ticket #2198: sha1cmd.c

File sha1cmd.c, 4.3 KB (added by opendarwin.org@…, 20 years ago)

sha1 command C file

Line 
1/*
2 * sha1cmd.c
3 * Copied from md5cmd.c 20040903 EH
4 *
5 * Copyright (c) 2002 - 2003 Apple Computer, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Apple Computer, Inc. nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#if HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37#include <tcl.h>
38
39#if HAVE_STRING_H
40#include <string.h>
41#endif
42
43#if defined(HAVE_LIBCRYPTO) && !defined(HAVE_LIBMD)
44
45/* Minimal wrapper around OpenSSL's libcrypto, as a compatibility
46 * layer for FreeBSD's libmd library.
47 * Originally written by: Rob Braun (bbraun) 1/18/2002
48 */
49
50#include <stdio.h>
51#include <fcntl.h>
52#include <stdlib.h>
53#include <unistd.h>
54#include <errno.h>
55
56#include <openssl/sha.h>
57
58#define SHA1Init(x) SHA1_Init(x)
59#define SHA1Update(x,y,z) SHA1_Update(x,y,z)
60#define SHA1Final(x,y) SHA1_Final(x,y)
61
62char *
63SHA1End(SHA_CTX *ctx, char *buf)
64{
65    int i;
66    unsigned char digest[SHA_DIGEST_LENGTH];
67    static const char hex[]="0123456789abcdef";
68
69    if (!buf)
70        buf = malloc(2*SHA_DIGEST_LENGTH + 1);
71    if (!buf)
72        return 0;
73    SHA1Final(digest, ctx);
74    for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
75        buf[i+i] = hex[digest[i] >> 4];
76        buf[i+i+1] = hex[digest[i] & 0x0f];
77    }
78    buf[i+i] = '\0';
79    return buf;
80}
81
82char *SHA1File(const char *filename, char *buf)
83{
84    unsigned char buffer[BUFSIZ];
85    SHA_CTX ctx;
86    int f,i,j;
87
88    SHA1Init(&ctx);
89    f = open(filename,O_RDONLY);
90    if (f < 0) return 0;
91    while ((i = read(f,buffer,sizeof buffer)) > 0) {
92        SHA1Update(&ctx,buffer,i);
93    }
94    j = errno;
95    close(f);
96    errno = j;
97    if (i < 0) return 0;
98    return SHA1End(&ctx, buf);
99}
100
101char *SHA1Data(const unsigned char *data, unsigned int len, char *buf)
102{
103        SHA_CTX ctx;
104        SHA1Init(&ctx);
105        SHA1Update(&ctx, data, len);
106        return SHA1End(&ctx, buf);
107}
108#elif defined(HAVE_LIBMD)
109#include <sys/types.h>
110#include <sha1.h>
111#else
112#error libcrypto or libmd are required
113#endif
114
115int SHA1Cmd(ClientData clientData UNUSED, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
116{
117        char *file, *action;
118        char buf[2*SHA_DIGEST_LENGTH + 1];
119        const char usage_message[] = "Usage: sha1 file ?file?";
120        const char error_message[] = "Could not open file: ";
121        Tcl_Obj *tcl_result;
122
123        if (objc != 3) {
124                Tcl_WrongNumArgs(interp, 1, objv, "action ?file?");
125                return TCL_ERROR;
126        }
127
128        /*
129         * Only the 'file' action is currently supported
130         */
131        action = Tcl_GetString(objv[1]);
132        if (strcmp(action, "file") != 0) {
133                tcl_result = Tcl_NewStringObj(usage_message, sizeof(usage_message) - 1);
134                Tcl_SetObjResult(interp, tcl_result);
135                return TCL_ERROR;
136        }
137        file = Tcl_GetString(objv[2]);
138
139        if (!SHA1File(file, buf)) {
140                tcl_result = Tcl_NewStringObj(error_message, sizeof(error_message) - 1);
141                Tcl_AppendObjToObj(tcl_result, Tcl_NewStringObj(file, -1));
142                Tcl_SetObjResult(interp, tcl_result);
143                return TCL_ERROR;
144        }
145        tcl_result = Tcl_NewStringObj(buf, sizeof(buf) - 1);
146        Tcl_SetObjResult(interp, tcl_result);
147        return TCL_OK;
148}