ver
[libexssl.git] / test / test01.c
blob8f33093b3af11c5c9a16f0decc30c5ad80d51736
1 #include <string.h>
2 #include <stdio.h>
3 #include <libex/str.h>
4 #include "ssl.h"
6 #define PRIV_KEY "-----BEGIN RSA PRIVATE KEY-----\n" \
7 "MIICXQIBAAKBgQDagE7sF2hON701d2KtUUxZOQBFW9PVzSR7rRdBQ3FJ71UhxXyL\n" \
8 "0kmSmFrv8OOqc4jH+1C4RULOjfcLG11oGINVi7Wsh4AH+21+RIarkRYvPpivQ7/b\n" \
9 "jlo7TdGFn2eFJKSWmjDNy9IE65qVG7qTYUhmHbTQbgs79/Gubnxw9fgZrwIDAQAB\n" \
10 "AoGBALyg4gE9H+XOhsBehUh8k72+0LYP1SuQwrsmLZpA389lFwhlleSbMLqmXnMf\n" \
11 "cVuYC/AgzImX+VmaAziKcjPIXa9m2mcpmGS7tpn0URIszGga0oWKtdetrdPkS89b\n" \
12 "GiMnkr6GLgyZVgPcQ7qtZOuH8lQXR0MaY+8efESetCaYdCGJAkEA8EI9+4SitIDM\n" \
13 "v3WSnYcKoh6vwz/3ABcFpPstxzprL+lfEh1vPaKXH5qapWsoftgl92c3X79Hwapg\n" \
14 "a6zZx3ZkowJBAOjRIs+DIkFP83uYo+5ysQPUUOpuwEYA7FIl2aAWxlthrNHEwsmF\n" \
15 "U7JPVtcNa0UNgWv3J3wZmqe33QSDZV6u+4UCQQDSQZZOq5dcEa+dCf5h/1EN6X71\n" \
16 "ht/Y40zcQbihNn2hM0Ew8DNupABO9xTJGMsannwzU8/A6fuY/0pcUKtRhEmzAkBL\n" \
17 "jbMSTKgNMfj+Hybz9txjNb+clJxpG1uVvRrGzR1KAoSm/oeIBnCG6SBxYbVyD1P0\n" \
18 "yZxStaxuyUg8creu5nJRAkBk5NW+bsO6HENwg/xiWSNXnR+3GJQoLyN9XM4pC5OA\n" \
19 "LUQcr02lKiLL/xofdeFzBEVHb1EdvNqYdLX2665DVXmc\n" \
20 "-----END RSA PRIVATE KEY-----\n"
21 #define PUB_KEY "-----BEGIN PUBLIC KEY-----\n" \
22 "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDagE7sF2hON701d2KtUUxZOQBF\n" \
23 "W9PVzSR7rRdBQ3FJ71UhxXyL0kmSmFrv8OOqc4jH+1C4RULOjfcLG11oGINVi7Ws\n" \
24 "h4AH+21+RIarkRYvPpivQ7/bjlo7TdGFn2eFJKSWmjDNy9IE65qVG7qTYUhmHbTQ\n" \
25 "bgs79/Gubnxw9fgZrwIDAQAB\n" \
26 "-----END PUBLIC KEY-----\n"
28 int main (int argc, const char *argv[]) {
29 if (argc != 2) return 1;
30 if (-1 == ssl_init()) return 1;
31 const char *src_str = argv[1];
32 size_t src_len = strlen(src_str);
33 if (src_len > 127) return 1;
34 printf("source string: %s\n", src_str);
35 FILE *f_src = fmemopen(CONST_STR_LEN(PUB_KEY), "r");
36 if (f_src) {
37 ssl_key_t *pub_key = ssl_key_init();
38 if (0 == ssl_load_pub_fp(f_src, pub_key)) {
39 str_t *crp_str = NULL;
40 if (0 == ssl_encrypt_pub(pub_key, src_str, src_len, &crp_str)) {
41 save_file("./_1", crp_str->ptr, crp_str->len);
42 str_t *b64_str = str_base64_encode(crp_str->ptr, crp_str->len, 8);
43 STR_ADD_NULL(b64_str);
44 printf("encoded string: %s\n", b64_str->ptr);
45 FILE *f_dst = fmemopen(CONST_STR_LEN(PRIV_KEY), "r");
46 if (f_dst) {
47 ssl_key_t *priv_key = ssl_key_init();
48 if (0 == ssl_load_priv_fp(f_dst, NULL, priv_key)) {
49 str_t *ub64_str = str_base64_decode(b64_str->ptr, b64_str->len, 8), *dssl_str = NULL;
50 if (0 != cmpstr(crp_str->ptr, crp_str->len, ub64_str->ptr, ub64_str->len))
51 printf("NO\n");
52 if (0 == ssl_decrypt_priv(priv_key, ub64_str->ptr, ub64_str->len, &dssl_str)) {
53 printf("decoded string: %s\n", dssl_str->ptr);
55 free(dssl_str);
56 free(ub64_str);
58 ssl_free_key(priv_key);
59 fclose(f_dst);
61 free(b64_str);
62 free(crp_str);
65 ssl_free_key(pub_key);
66 fclose(f_src);
68 ssl_done();
69 return 0;