Rename ext/Digest/SHA to ext/Digest-SHA
[perl.git] / ext / Digest-SHA / src / shaxtra.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "sha.h"
4
5 static unsigned char *shacomp(alg, fmt, bitstr, bitcnt)
6 int alg;
7 int fmt;
8 unsigned char *bitstr;
9 unsigned long bitcnt;
10 {
11         SHA *s;
12         static unsigned char digest[SHA_MAX_HEX_LEN+1];
13         unsigned char *ret = digest;
14
15         if ((s = shaopen(alg)) == NULL)
16                 return(NULL);
17         shawrite(bitstr, bitcnt, s);
18         shafinish(s);
19         if (fmt == SHA_FMT_RAW)
20                 memcpy(digest, shadigest(s), s->digestlen);
21         else if (fmt == SHA_FMT_HEX)
22                 strcpy((char *) digest, shahex(s));
23         else if (fmt == SHA_FMT_BASE64)
24                 strcpy((char *) digest, shabase64(s));
25         else
26                 ret = NULL;
27         shaclose(s);
28         return(ret);
29 }
30
31 #define SHA_DIRECT(type, name, alg, fmt)                        \
32 type name(bitstr, bitcnt)                                       \
33 unsigned char *bitstr;                                          \
34 unsigned long bitcnt;                                           \
35 {                                                               \
36         return((type) shacomp(alg, fmt, bitstr, bitcnt));       \
37 }
38
39 SHA_DIRECT(unsigned char *, sha1digest, SHA1, SHA_FMT_RAW)
40 SHA_DIRECT(char *, sha1hex, SHA1, SHA_FMT_HEX)
41 SHA_DIRECT(char *, sha1base64, SHA1, SHA_FMT_BASE64)
42
43 SHA_DIRECT(unsigned char *, sha224digest, SHA224, SHA_FMT_RAW)
44 SHA_DIRECT(char *, sha224hex, SHA224, SHA_FMT_HEX)
45 SHA_DIRECT(char *, sha224base64, SHA224, SHA_FMT_BASE64)
46
47 SHA_DIRECT(unsigned char *, sha256digest, SHA256, SHA_FMT_RAW)
48 SHA_DIRECT(char *, sha256hex, SHA256, SHA_FMT_HEX)
49 SHA_DIRECT(char *, sha256base64, SHA256, SHA_FMT_BASE64)
50
51 SHA_DIRECT(unsigned char *, sha384digest, SHA384, SHA_FMT_RAW)
52 SHA_DIRECT(char *, sha384hex, SHA384, SHA_FMT_HEX)
53 SHA_DIRECT(char *, sha384base64, SHA384, SHA_FMT_BASE64)
54
55 SHA_DIRECT(unsigned char *, sha512digest, SHA512, SHA_FMT_RAW)
56 SHA_DIRECT(char *, sha512hex, SHA512, SHA_FMT_HEX)
57 SHA_DIRECT(char *, sha512base64, SHA512, SHA_FMT_BASE64)