2 \brief Password Encyption and Storing
3 \details hashes password using SHA256 and saves result in a file on VFS using
4 hashing algorithm provided by wolfcrypt(wc), a subsystem of wolfSSL
7 -----------------------------------------------------------------------------*/
10 #include <wolfssl/wolfcrypt/sha256.h>
11 #include <emscripten.h>
13 word32
str_len(const char*);
14 int auth_encrypt(int argc
, char** argv
);
15 void store_password(byte
*);
17 /* Should come with wolfcrypt type.h */
18 /* typedef unsigned int word32; */
21 str_len (const char* str
)
25 for (strl
= str
; *strl
!= '\0'; ++strl
)
27 /* nothing to be done */;
33 /* Takes a string and hashes it, returning
34 the encrypted string as a result */
37 auth_encrypt(int argc
, char** argv
)
39 /* Determine length of password */
42 byte hash
[32]; /* 32bytes * 8 = 256, the # bits of hash */
43 const unsigned char c_password
[32]; /* Passwords are between 8-32 chars */
44 Sha256 sha256
[1]; /* Init a Sha256 type for wc */
48 len
= str_len(password
);
50 /* Converting password to unsigned const char* for wc_functions */
51 memcpy( (void*) c_password
, (void*) password
, len
+1);
53 if (wc_InitSha256(sha256
) != 0 )
55 printf("wc_InitSha256 failed\n");
59 wc_Sha256Update(sha256
, c_password
, len
);
60 wc_Sha256Final(sha256
, hash
);
63 printf("password is: %s\n", c_password
);
64 printf("password length is: %d\n", len
);
65 for(len
= 0; len
< 32; len
++){
66 printf("hash is %x\n",hash
[len
]);
70 free((void*) c_password
);
77 store_password(byte
* hash
)
80 /* Save hash to a file on emscripten VFS */
85 printf("=====Storing Password in auth/password========\n");
86 /* for(i = 0; i < 32; i++){ */
87 /* printf("hash is %x\n",hash[i]); */
90 fp
= fopen("/auth/password", "w+");
93 printf("cannot open file\n");
97 fwrite(hash
, 1, 32, fp
);
100 printf("========Stored Password=========\n");
102 fseek(fp
, 0, SEEK_SET
);
104 for(i
= 0; i
< 32; i
++)
106 fread(buff
, 1, 32, fp
) ;
107 printf("buff is %x\n",buff
[i
]);