forked from maze-runnar/mqtt-cpp-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls_enryption.c
94 lines (68 loc) · 2.1 KB
/
tls_enryption.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <string.h>
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#else
#include <stdlib.h>
#include <stdio.h>
#define mbedtls_printf printf
#define mbedtls_exit exit
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
#endif
#if defined(MBEDTLS_MD5_C)
#include "mbedtls/md5.h"
#include "mbedtls/sha512.h"
#include "mbedtls/sha256.h"
#endif
#if !defined(MBEDTLS_MD5_C)
int main( void )
{
mbedtls_printf("MBEDTLS_MD5_C not defined.\n");
mbedtls_exit( 0 );
}
#else
static void dump_buf( const char *title, unsigned char *buf, size_t len )
{
size_t i;
mbedtls_printf( "%s", title );
for( i = 0; i < len; i++ )
mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
"0123456789ABCDEF" [buf[i] % 16] );
mbedtls_printf( "\n" );
}
int main( void )
{
int i, ret;
unsigned char digest[16];
unsigned char hash[32];
char str[1000] = "I";
printf("%s\n", "Enter string for encryption: ");
fgets(str, sizeof(str), stdin); // read string
str[strcspn(str, "\n")] = 0; // ignoring new line while reading input using fgets
mbedtls_printf( "MD5(\"%s\") = ", str );
//md5 encryption using tls
if( ( ret = mbedtls_md5_ret( (unsigned char *) str, 1000, digest ) ) != 0 )
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
for( i = 0; i < 16; i++ )
mbedtls_printf( "%02x", digest[i] );
mbedtls_printf( "\n" );
//sha-256 encryption using tls
if( ( ret = mbedtls_sha256_ret( str, sizeof( str ), hash, 0 ) ) != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_sha256_ret returned %d\n", ret );
}
dump_buf( "SHA-256: ", hash, sizeof( hash ) );
// sha-512 encryption using tls
if( ( ret = mbedtls_sha512_ret( str, sizeof( str ), hash, 0 ) ) != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_sha512_ret returned %d\n", ret );
}
dump_buf( "SHA-512: ", hash, sizeof( hash ) );
mbedtls_exit( MBEDTLS_EXIT_SUCCESS );
}
#endif