5738b09e48b36dde421cda348a3132d61c69988f
[openssl.git] / perl / openssl_digest.xs
1 #include "p5SSLeay.h"
2
3 int boot_digest()
4         {
5         SSLeay_add_all_digests();
6         return(1);
7         }
8
9 MODULE =  SSLeay::MD    PACKAGE = SSLeay::MD    PREFIX = p5_EVP_MD_
10
11 PROTOTYPES: ENABLE
12 VERSIONCHECK: DISABLE
13
14 # SSLeay::MD::new(name) name= md2, md5, sha, sha1, or mdc2
15 #       md->name() - returns the name
16 #       md->init() - reinitalises the digest
17 #       md->update(data) - adds more data to digest
18 #       digest=md->final() - returns digest
19 #
20
21 void
22 p5_EVP_MD_new(...)
23         PREINIT:
24                 EVP_MD_CTX *ctx;
25                 EVP_MD *md;
26                 char *name;
27         PPCODE:
28                 if ((items == 1) && SvPOK(ST(0)))
29                         name=SvPV(ST(0),na);
30                 else if ((items == 2) && SvPOK(ST(1)))
31                         name=SvPV(ST(1),na);
32                 else
33                         croak("Usage: SSLeay::MD::new(type)");
34                 PUSHs(sv_newmortal());
35                 md=EVP_get_digestbyname(name);
36                 if (md != NULL)
37                         {
38                         ctx=malloc(sizeof(EVP_MD_CTX));
39                         EVP_DigestInit(ctx,md);
40                         sv_setref_pv(ST(0), "SSLeay::MD", (void*)ctx);
41                         }
42
43 datum
44 p5_EVP_MD_name(ctx)
45         EVP_MD_CTX *ctx
46         CODE:
47                 RETVAL.dptr=OBJ_nid2ln(EVP_MD_type(EVP_MD_CTX_type(ctx)));
48                 RETVAL.dsize=strlen(RETVAL.dptr);
49         OUTPUT:
50                 RETVAL
51         
52 void
53 p5_EVP_MD_init(ctx)
54         EVP_MD_CTX *ctx
55         CODE:
56                 EVP_DigestInit(ctx,EVP_MD_CTX_type(ctx));
57
58 void
59 p5_EVP_MD_update(ctx, in)
60         EVP_MD_CTX *ctx
61         datum in
62         CODE:
63                 EVP_DigestUpdate(ctx,in.dptr,in.dsize);
64
65 datum
66 p5_EVP_MD_final(ctx)
67         EVP_MD_CTX *ctx
68         PREINIT:
69                 char md[EVP_MAX_MD_SIZE];
70                 int len;
71         CODE:
72                 EVP_DigestFinal(ctx,md,&len);
73                 RETVAL.dptr=md;
74                 RETVAL.dsize=len;
75         OUTPUT:
76                 RETVAL
77
78 void
79 p5_EVP_MD_DESTROY(ctx)
80         EVP_MD_CTX *ctx
81         CODE:
82         free((char *)ctx);
83