summaryrefslogtreecommitdiffstats
path: root/src/cls_crypto.cc
blob: 2c93e06e7ca8aeb45f48515c0bb38b318cecb96a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <openssl/md5.h>
#include <openssl/sha.h>

#include "include/types.h"
#include "objclass/objclass.h"

CLS_VER(1,0)
CLS_NAME(crypto)

int md5_method(cls_method_context_t ctx, char *indata, int datalen,
				 char **outdata, int *outdatalen)
{
   MD5_CTX c;
   unsigned char *md;

   cls_log("md5 method");
   cls_log("indata=%.*s data_len=%d", datalen, indata, datalen);

   md = (unsigned char *)cls_alloc(MD5_DIGEST_LENGTH);
   if (!md)
     return -ENOMEM;

   MD5_Init(&c);
   MD5_Update(&c, indata, (unsigned long)datalen);
   MD5_Final(md,&c);

   *outdata = (char *)md;
   *outdatalen = MD5_DIGEST_LENGTH;

   return 0;
}

int sha1_method(cls_method_context_t ctx, char *indata, int datalen,
				 char **outdata, int *outdatalen)
{
   SHA_CTX c;
   unsigned char *md;

   cls_log("sha1 method");
   cls_log("indata=%.*s data_len=%d", datalen, indata, datalen);

   md = (unsigned char *)cls_alloc(SHA_DIGEST_LENGTH);
   if (!md)
     return -ENOMEM;

   SHA1_Init(&c);
   SHA1_Update(&c, indata, (unsigned long)datalen);
   SHA1_Final(md,&c);

   *outdata = (char *)md;
   *outdatalen = SHA_DIGEST_LENGTH;

   return 0;
}

CLS_INIT(crypto)
{
   cls_log("Loaded crypto class!");

   cls_handle_t h_class;
   cls_method_handle_t h_md5;
   cls_method_handle_t h_sha1;

   cls_register("crypto", &h_class);
   cls_register_method(h_class, "md5", CLS_METHOD_RD, md5_method, &h_md5);
   cls_register_method(h_class, "sha1", CLS_METHOD_RD, sha1_method, &h_sha1);

   return;
}