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>
12 #include <emscripten.h>
15 word32
str_len(const char*);
16 int auth_encrypt(int argc
, char** argv
);
17 void store_password(byte
*);
19 /* Should come with wolfcrypt type.h */
20 /* typedef unsigned int word32; */
23 str_len (const char* str
)
27 for (strl
= str
; *strl
!= '\0'; ++strl
)
29 /* nothing to be done */;
35 /* Takes a string and hashes it, returning
36 the encrypted string as a result */
39 auth_encrypt(int argc
, char** argv
)
41 /* Determine length of password */
44 byte hash
[32]; /* 32bytes * 8 = 256, the # bits of hash */
45 const unsigned char c_password
[32]; /* Passwords are between 8-32 chars */
46 Sha256 sha256
[1]; /* Init a Sha256 type for wc */
50 len
= str_len(password
);
52 /* Converting password to unsigned const char* for wc_functions */
53 memcpy( (void*) c_password
, (void*) password
, len
+1);
55 if (wc_InitSha256(sha256
) != 0 )
57 printf("wc_InitSha256 failed\n");
61 wc_Sha256Update(sha256
, c_password
, len
);
62 wc_Sha256Final(sha256
, hash
);
65 printf("password is: %s\n", c_password
);
66 printf("password length is: %d\n", len
);
67 for(len
= 0; len
< 32; len
++){
68 printf("hash is %x\n",hash
[len
]);
72 //free((void*) c_password);
79 store_password(byte
* hash
)
82 /* Save hash to a file on emscripten VFS */
87 printf("=====Storing Password in auth/password========\n");
88 /* for(i = 0; i < 32; i++){ */
89 /* printf("hash is %x\n",hash[i]); */
92 fp
= fopen("/auth/password", "w+");
95 printf("cannot open file\n");
99 fwrite(hash
, 1, 32, fp
);
102 printf("========Stored Password=========\n");
104 fseek(fp
, 0, SEEK_SET
);
106 for(i
= 0; i
< 32; i
++)
108 fread(buff
, 1, 32, fp
) ;
109 printf("buff is %x\n",buff
[i
]);