duobango-tinySAK,20121214

阅读版本:2.0

sha1(tsk_sha1.h)

接口:
tsk_sha1_errcode_t tsk_sha1compute(const char* input, tsk_size_t size, tsk_sha1string_t *result);

在此此接口实现中,使用string中hex值转为字符串的函数接口tsk_str_from_hex

结合uuid来看此接口的应用

time(tsk_time.h)

接口:
uint64_t tsk_time_now();

返回当前时间的无符号整数

UUID(tsk_uuid.h)

接口:
int tsk_uuidgenerate(tsk_uuidstring_t *result);

在此接口实现中应用上述两个接口,看下源代码:

 1 /**@defgroup tsk_uuid_group niversally Unique Identifier (UUID version 5) implementation (RFC 4122).
 2 */
 3 
 4 /**@ingroup tsk_uuid_group
 5 */
 6 int tsk_uuidgenerate(tsk_uuidstring_t *result)
 7 {
 8     /* From wikipedia
 9     *    Version 5 UUIDs use a scheme with SHA-1 hashing, otherwise it is the same idea as in version 3.
10     *    RFC 4122 states that version 5 is preferred over version 3 name based UUIDs.
11     *    Note that the 160 bit SHA-1 hash is truncated to 128 bits to make the length work out.
12     */
13     tsk_sha1string_t sha1result;
14     tsk_istr_t now;
15     unsigned i, k;
16     static char HEX[] = "0123456789abcdef";
17 
18     tsk_itoa(tsk_time_now(), &now);// tsk_itoa用法 19     tsk_sha1compute(now, sizeof(now), &sha1result);// tsk_sha1compute用法 20 
21     /* XOR the SHA-1 result with random numbers. */
22     for(i=0; i<(TSK_UUID_DIGEST_SIZE*2); i+=4){
23 #if 0
24         *((uint32_t*)&sha1result[i]) ^= rand();
25 #else
26         k = rand();
27         sha1result[i] ^= k, sha1result[i + 1] ^= k,
28         sha1result[i + 2] ^= k, sha1result[i + 3] ^= k;
29 #endif
30         
31         for(k=0; k<sizeof(uint32_t); k++){
32             sha1result[i+k] = HEX[sha1result[i+k] & 0x0F]; /* To hexa. */
33         }
34     }
35 
36     /* f47ac10b-58cc-4372-a567-0e02b2c3d479 */
37     memcpy(&(*result)[0], &sha1result[0], 8);
38     (*result)[8] = '-';
39 
40     memcpy(&(*result)[9], &sha1result[8], 4);
41     (*result)[13] = '-';
42 
43     memcpy(&(*result)[14], &sha1result[12], 4);
44     (*result)[18] = '-';
45 
46     memcpy(&(*result)[19], &sha1result[16], 4);
47     (*result)[23] = '-';
48 
49     memcpy(&(*result)[24], &sha1result[20], 12);
50     (*result)[36] = '\0';
51 
52     return 0;
53 }

 



md5(tsk_md5.h)

接口:
int tsk_md5compute(const char* input, tsk_size_t size, tsk_md5string_t *result);

eg,简单调用:
tsk_md5compute(msgs_md5[i].msg, strlen(msgs_md5[i].msg), &md5result);


base64(tsk_base64.h)

接口:
tsk_size_t tsk_base64_encode(const uint8_t* input, tsk_size_t input_size, char **output);
tsk_size_t tsk_base64_decode(const uint8_t* input, tsk_size_t input_size, char **output);

eg.
size = tsk_base64_encode((const uint8_t*)b64_msgs[i].ascii, strlen(b64_msgs[i].ascii), &output_e);


HMAC(tsk_hmac.h)
HMAC是密钥相关的哈希运算消息认证码(Hash-based Message Authentication Code),HMAC运算利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出。

接口:
int hmac_md5_compute(const uint8_t* input, tsk_size_t input_size, const char* key, tsk_size_t key_size, tsk_md5string_t *result);

int hmac_md5digest_compute(const uint8_t* input, tsk_size_t input_size, const char* key, tsk_size_t key_size, tsk_md5digest_t result);

int hmac_sha1_compute(const uint8_t* input, tsk_size_t input_size, const char* key, tsk_size_t key_size, tsk_sha1string_t *result);

int hmac_sha1digest_compute(const uint8_t* input, tsk_size_t input_size, const char* key, tsk_size_t key_size, tsk_sha1digest_t result);

【备注】tinySAK部分的代码就阅读到此,其他源码后续应用中阅读
比如,线程,timer,互斥锁等,可以在实际中应用,也可以依据平台使用该平台下提供的对象...

努力学习...

你可能感兴趣的:(duobango-tinySAK,20121214)