mdXhl.c revision 1.2
11.2Sthorpej/* $NetBSD: mdXhl.c,v 1.2 1997/04/30 00:40:47 thorpej Exp $ */ 21.1Sthorpej 31.2Sthorpej/* 41.1Sthorpej * ---------------------------------------------------------------------------- 51.1Sthorpej * "THE BEER-WARE LICENSE" (Revision 42): 61.1Sthorpej * <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you 71.1Sthorpej * can do whatever you want with this stuff. If we meet some day, and you think 81.1Sthorpej * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 91.1Sthorpej * ---------------------------------------------------------------------------- 101.1Sthorpej * 111.1Sthorpej * from FreeBSD Id: mdXhl.c,v 1.8 1996/10/25 06:48:12 bde Exp 121.2Sthorpej */ 131.2Sthorpej 141.2Sthorpej/* 151.2Sthorpej * Modifed April 29, 1997 by Jason R. Thorpe <thorpej@netbsd.org> 161.1Sthorpej */ 171.1Sthorpej 181.1Sthorpej#include <sys/types.h> 191.1Sthorpej#include <fcntl.h> 201.1Sthorpej#include <unistd.h> 211.1Sthorpej 221.1Sthorpej#include <errno.h> 231.1Sthorpej#include <stdio.h> 241.1Sthorpej#include <stdlib.h> 251.1Sthorpej 261.2Sthorpej#define CONCAT(x,y) __CONCAT(x,y) 271.2Sthorpej#define MDNAME(x) CONCAT(MDALGORITHM,x) 281.1Sthorpej 291.1Sthorpejchar * 301.2SthorpejMDNAME(End)(ctx, buf) 311.2Sthorpej MDNAME(_CTX) *ctx; 321.2Sthorpej char *buf; 331.1Sthorpej{ 341.2Sthorpej int i; 351.2Sthorpej unsigned char digest[16]; 361.2Sthorpej static const char hex[]="0123456789abcdef"; 371.2Sthorpej 381.2Sthorpej if (buf == NULL) 391.2Sthorpej buf = malloc(33); 401.2Sthorpej if (buf == NULL) 411.2Sthorpej return (NULL); 421.2Sthorpej 431.2Sthorpej MDNAME(Final)(digest, ctx); 441.2Sthorpej 451.2Sthorpej for (i = 0; i < 16; i++) { 461.2Sthorpej buf[i+i] = hex[digest[i] >> 4]; 471.2Sthorpej buf[i+i+1] = hex[digest[i] & 0x0f]; 481.2Sthorpej } 491.2Sthorpej 501.2Sthorpej buf[i+i] = '\0'; 511.2Sthorpej return (buf); 521.1Sthorpej} 531.1Sthorpej 541.1Sthorpejchar * 551.2SthorpejMDNAME(File)(filename, buf) 561.2Sthorpej const char *filename; 571.2Sthorpej char *buf; 581.1Sthorpej{ 591.2Sthorpej unsigned char buffer[BUFSIZ]; 601.2Sthorpej MDNAME(_CTX) ctx; 611.2Sthorpej int f, i, j; 621.2Sthorpej 631.2Sthorpej MDNAME(Init)(&ctx); 641.2Sthorpej f = open(filename, O_RDONLY, 0666); 651.2Sthorpej if (f < 0) 661.2Sthorpej return NULL; 671.2Sthorpej 681.2Sthorpej while ((i = read(f, buffer, sizeof(buffer))) > 0) 691.2Sthorpej MDNAME(Update)(&ctx, buffer, i); 701.2Sthorpej 711.2Sthorpej j = errno; 721.2Sthorpej close(f); 731.2Sthorpej errno = j; 741.2Sthorpej 751.2Sthorpej if (i < 0) 761.2Sthorpej return NULL; 771.2Sthorpej 781.2Sthorpej return (MDNAME(End)(&ctx, buf)); 791.1Sthorpej} 801.1Sthorpej 811.1Sthorpejchar * 821.2SthorpejMDNAME(Data)(data, len, buf) 831.2Sthorpej const unsigned char *data; 841.2Sthorpej unsigned int len; 851.2Sthorpej char *buf; 861.1Sthorpej{ 871.2Sthorpej MDNAME(_CTX) ctx; 881.1Sthorpej 891.2Sthorpej MDNAME(Init)(&ctx); 901.2Sthorpej MDNAME(Update)(&ctx, data, len); 911.2Sthorpej return (MDNAME(End)(&ctx, buf)); 921.1Sthorpej} 93