Browse Source

Add SHA1End, SHA1File, SHA1Data helper functions like in md5(3).

OPENBSD_2_2
millert 27 years ago
parent
commit
0818e080d8
4 changed files with 156 additions and 10 deletions
  1. +4
    -1
      src/include/sha1.h
  2. +2
    -2
      src/lib/libc/hash/Makefile.inc
  3. +70
    -7
      src/lib/libc/hash/sha1.3
  4. +80
    -0
      src/lib/libc/hash/sha1hl.c

+ 4
- 1
src/include/sha1.h View File

@ -1,4 +1,4 @@
/* $OpenBSD: sha1.h,v 1.6 1997/07/10 23:37:49 millert Exp $ */
/* $OpenBSD: sha1.h,v 1.7 1997/07/12 20:06:01 millert Exp $ */
/*
* SHA-1 in C
@ -19,5 +19,8 @@ void SHA1Transform __P((u_int32_t state[5], u_char buffer[64]));
void SHA1Init __P((SHA1_CTX *context));
void SHA1Update __P((SHA1_CTX *context, u_char *data, u_int len));
void SHA1Final __P((u_char digest[20], SHA1_CTX *context));
char *SHA1End __P((SHA1_CTX *, char *));
char *SHA1File __P((char *, char *));
char *SHA1Data __P((const u_char *, size_t, char *));
#endif /* _SHA1_H */

+ 2
- 2
src/lib/libc/hash/Makefile.inc View File

@ -1,9 +1,9 @@
# $OpenBSD: Makefile.inc,v 1.2 1997/07/11 04:29:20 millert Exp $
# $OpenBSD: Makefile.inc,v 1.3 1997/07/12 20:06:02 millert Exp $
# hash functions
.PATH: ${.CURDIR}/hash
SRCS+= sha1.c
SRCS+= sha1.c sha1hl.c
MAN+= sha1.3
MLINKS+=sha1.3 SHA1Init.3
MLINKS+=sha1.3 SHA1Update.3


+ 70
- 7
src/lib/libc/hash/sha1.3 View File

@ -1,4 +1,4 @@
.\" $OpenBSD: sha1.3,v 1.3 1997/07/12 11:02:28 provos Exp $
.\" $OpenBSD: sha1.3,v 1.4 1997/07/12 20:06:03 millert Exp $
.\"
.\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
.\" All rights reserved.
@ -36,7 +36,10 @@
.Nm SHA1Init ,
.Nm SHA1Update ,
.Nm SHA1Final ,
.Nm SHA1Transform
.Nm SHA1Transform ,
.Nm SHA1End ,
.Nm SHA1File ,
.Nm SHA1Data ,
.Nd calculate the NIST Secure Hash Algorithm
.Sh SYNOPSIS
.Fd #include <sys/types.h>
@ -49,6 +52,12 @@
.Fn SHA1Final "u_char digest[20]" "SHA1_CTX *context"
.Ft void
.Fn SHA1Transform "u_int32_t state[5]" "u_char buffer[64]"
.Ft "char *"
.Fn SHA1End "SHA1_CTX *context" "char *buf"
.Ft "char *"
.Fn SHA1File "char *filename" "char *buf"
.Ft "char *"
.Fn SHA1Data "u_char *data" "u_int len" "char *buf"
.Sh DESCRIPTION
The SHA1 functions implement then NIST Secure Hash Algorithm (SHA-1),
FIPS PUB 180-1. SHA-1 is used to generate a condensed representation
@ -64,7 +73,7 @@ functions with which they share a similar interface.
.Pp
The
.Fn SHA1Init
function initializes a MDX_CTX
function initializes a SHA1_CTX
.Ar context
for use with
.Fn SHA1Update ,
@ -103,7 +112,44 @@ and
instead of calling
.Fn SHA1Transform
directly.
.Sh EXAMPLE
.Pp
The
.Fn SHA1End
function is a front end for
.Fn SHA1Final
which converts the digest into an
.Tn ASCII
representation of the 160 bit digest in hexadecimal.
.Pp
The
.Fn SHA1File
function calculates the digest for a file and returns the result via
.Fn SHA1End .
If
.Fn SHA1File
is unable to open the file a NULL pointer is returned.
.Pp
The
.Fn SHA1Data
function
calculates the digest of an arbitrary string and returns the result via
.Fn SHA1End .
.Pp
For each of the
.Fn SHA1End ,
.Fn SHA1File ,
and
.Fn SHA1Data
functions the
.Ar buf
parameter should either be a string of at least 41 characters in
size or a NULL pointer. In the latter case, space will be dynamically
allocated via
.Xr malloc 3
and should be freed using
.Xr free 3
when it is no longer needed.
.Sh EXAMPLES
The follow code fragment will calculate the digest for
the string "abc" which is ``0xa9993e36476816aba3e25717850c26c9cd0d89d''.
.Bd -literal -offset indent
@ -122,7 +168,16 @@ SHA1Final(results, &sha);
printf("0x");
for (n = 0; n < 20; n++)
printf("%x", results[n]);
putchar('\n');
putchar('\\n');
.Ed
.Pp
Alternately, the helper functions could be used in the following way:
.Bd -literal -offset indent
SHA1_CTX sha;
u_char output[41];
char *buf = "abc";
printf("0x%s", MD5Data(buf, strlen(buf), output));
.Ed
.Sh CAVEATS
This implementation of SHA-1 has not been validated by NIST
@ -132,9 +187,17 @@ If a message digest is to be copied to a multi-byte type (ie:
an array of five 32-bit integers) it will be necessary to
perform byte swapping on little endian machines such as the i386, alpha,
and vax.
.Sh AUTHOR
This implementation of SHA-1 was written by Steve Reid <steve@edmweb.com>.
.Sh AUTHORS
This implementation of SHA-1 was written by Steve Reid.
.br
The
.Fn SHA1End ,
.Fn SHA1File ,
and
.Fn SHA1Data
helper functions are derived from code written by Poul-Henning Kamp.
.Sh SEE ALSO
.Xr sha1 1 ,
.Xr md4 3 ,
.Xr md5 3
.Pp


+ 80
- 0
src/lib/libc/hash/sha1hl.c View File

@ -0,0 +1,80 @@
/* sha1hl.c
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
* ----------------------------------------------------------------------------
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char rcsid[] = "$OpenBSD: sha1hl.c,v 1.1 1997/07/12 20:06:03 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <sha1.h>
/* ARGSUSED */
char *
SHA1End(ctx, buf)
SHA1_CTX *ctx;
char *buf;
{
int i;
char *p = buf;
u_char digest[20];
static const char hex[]="0123456789abcdef";
if (p == NULL && (p = malloc(41)) == NULL)
return 0;
SHA1Final(digest,ctx);
for (i = 0; i < 20; i++) {
p[i + i] = hex[digest[i] >> 4];
p[i + i + 1] = hex[digest[i] & 0x0f];
}
p[i + i] = '\0';
return(p);
}
char *
SHA1File (filename, buf)
char *filename;
char *buf;
{
u_char buffer[BUFSIZ];
SHA1_CTX ctx;
int fd, num, oerrno;
SHA1Init(&ctx);
if ((fd = open(filename,O_RDONLY)) < 0)
return(0);
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
SHA1Update(&ctx, buffer, num);
oerrno = errno;
close(fd);
errno = oerrno;
return(num < 0 ? 0 : SHA1End(&ctx, buf));
}
char *
SHA1Data (data, len, buf)
const u_char *data;
size_t len;
char *buf;
{
SHA1_CTX ctx;
SHA1Init(&ctx);
SHA1Update(&ctx, data, len);
return(SHA1End(&ctx, buf));
}

Loading…
Cancel
Save