/*!@file \brief Password Encyption and Storing \details hashes password using SHA256 and saves result in a file on VFS using hashing algorithm provided by wolfcrypt(wc), a subsystem of wolfSSL \author JEL \date 2016 -----------------------------------------------------------------------------*/ #include #include #include #ifdef __EMSCRIPTEN__ #include #endif word32 str_len(const char*); int auth_encrypt(int argc, char** argv); void store_password(byte*); /* Should come with wolfcrypt type.h */ /* typedef unsigned int word32; */ word32 str_len (const char* str) { const char* strl; for (strl = str; *strl != '\0'; ++strl) { /* nothing to be done */; } return strl - str; } /* Takes a string and hashes it, returning the encrypted string as a result */ int auth_encrypt(int argc, char** argv) { /* Determine length of password */ word32 len = 0; char* password; byte hash[32]; /* 32bytes * 8 = 256, the # bits of hash */ const unsigned char c_password[32]; /* Passwords are between 8-32 chars */ Sha256 sha256[1]; /* Init a Sha256 type for wc */ password = argv[0]; len = str_len(password); /* Converting password to unsigned const char* for wc_functions */ memcpy( (void*) c_password, (void*) password, len+1); if (wc_InitSha256(sha256) != 0 ) { printf("wc_InitSha256 failed\n"); } else { wc_Sha256Update(sha256, c_password, len); wc_Sha256Final(sha256, hash); } printf("password is: %s\n", c_password); printf("password length is: %d\n", len); for(len = 0; len < 32; len++){ printf("hash is %x\n",hash[len]); } store_password(hash); //free((void*) c_password); return 0; } void store_password(byte* hash) { byte b; /* Save hash to a file on emscripten VFS */ FILE *fp; int i; byte buff[32] = {0}; printf("=====Storing Password in auth/password========\n"); /* for(i = 0; i < 32; i++){ */ /* printf("hash is %x\n",hash[i]); */ /* } */ fp = fopen("/auth/password", "w+"); if(!fp) { printf("cannot open file\n"); } else { fwrite(hash, 1, 32, fp); } printf("========Stored Password=========\n"); fseek(fp, 0, SEEK_SET); for(i = 0; i < 32; i++) { fread(buff, 1, 32, fp) ; printf("buff is %x\n",buff[i]); } fclose(fp); }