OSDN Git Service

Initial contents for ntch.
[ntch/develop.git] / src / utils / crypt.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <openssl/sha.h>
6
7 #include "utils/nt_std_t.h"
8 #include "utils/base64.h"
9 #include "utils/crypt.h"
10
11
12 BOOL nt_make_sha1_string(const char *org_name, 
13                         char *outbuf, size_t outbuf_len)
14 {
15         assert(outbuf != NULL);
16         assert(outbuf_len > SHA_DIGEST_LENGTH*2+1);
17         assert(org_name != NULL);
18
19         SHA_CTX ctx;
20         unsigned char md[SHA_DIGEST_LENGTH]; 
21         char *outptr;
22         int i , len;
23         
24         SHA1_Init(&ctx);
25
26         len =  strlen(org_name);
27         
28         SHA1_Update(&ctx, org_name, len+1);
29
30         SHA1_Final(&(md[0]), &ctx);
31  
32         outptr = outbuf;
33         for(i = 0; i < SHA_DIGEST_LENGTH; i++){
34                 sprintf(outptr, "%02x", md[i]);
35                 outptr += 2;
36         }
37         while(outptr < (outbuf+outbuf_len)){
38                 *outptr = '\0';
39                 outptr++;
40         }
41         return TRUE;
42 }