diff --git a/README.md b/README.md index 6dbef6c..dee599f 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A bcrypt plugin for samp in Rust. ## Installation ### sampctl If you are a sampctl user -`sampctl p install Sreyas-Sreelal/samp-bcrypt` +`sampctl install Sreyas-Sreelal/samp-bcrypt` ### OR * Download suitable binary files from releases for your operating system @@ -34,68 +34,79 @@ If you are a sampctl user `make run` ## API -* #### bcrypt_hash(playerid, const callback[], const input[],cost,const args[] = "", {Float, _}:...) +* #### bcrypt_hash(playerid, const callback[], const input[], cost, const args[] = "", {Float, _}:...) * `playerid` - id of the player * `callback[]` - callback to execute after hashing * `input[]` - string to hash * `cost` - work factor (4 - 31) - * `args` - custom arguments + * `args[]` - custom arguments **Example** ```Pawn - main(){ - bcrypt_hash(0,"OnPassswordHash","text",BCRYPT_COST); + main() + { + bcrypt_hash(0, "OnPassswordHash", "text", BCRYPT_COST); } forward OnPassswordHash(playerid); - public OnPassswordHash(playerid){ - //hashing completed + public OnPassswordHash(playerid) + { + // Hashing completed } ``` -* #### bcrypt_get_hash(dest[],size = sizeof(hash)) +* #### bcrypt_get_hash(dest[], size = sizeof(hash)) * `dest[]` - string to store hashed data * `size` - max size of dest string **Example** ```Pawn - main(){ - bcrypt_hash(0,"OnPassswordHash","text",BCRYPT_COST); + main() + { + bcrypt_hash(0, "OnPassswordHash", "text", BCRYPT_COST); } forward OnPassswordHash(playerid); - public OnPassswordHash(playerid){ + public OnPassswordHash(playerid) + { new dest[BCRYPT_HASH_LENGTH]; bcrypt_get_hash(dest); - printf("hash : %s",dest); + printf("hash : %s", dest); } ``` -* #### bcrypt_verify(playerid,callback[],input[],hash[]) +* #### bcrypt_verify(playerid, const callback[], const input[], const hash[], const args[] = "", {Float, _}:...) * `playerid` - id of the player * `callback[]` - callback to execute after hashing * `input[]` - text to compare with hash * `hash[]` - hash to compare with text - * `args` - custom arguments + * `args[]` - custom arguments **Example** ```Pawn - main(){ - bcrypt_hash(0,"OnPassswordHash","text",BCRYPT_COST); + main() + { + bcrypt_hash(0, "OnPassswordHash", "text", BCRYPT_COST); } forward OnPassswordHash(playerid); - public OnPassswordHash(playerid){ + public OnPassswordHash(playerid) + { new dest[BCRYPT_HASH_LENGTH]; bcrypt_get_hash(dest); - bcrypt_verify(playerid,"OnPassswordVerify","text",dest); + bcrypt_verify(playerid, "OnPassswordVerify", "text", dest); } - forward OnPassswordVerify(playerid,bool:success); - public OnPassswordVerify(playerid,bool:success){ - //success denotes verifying was successful or not - if(success){ - //verfied - } else{ - //hash doesn't match with text + forward OnPassswordVerify(playerid, bool:success); + public OnPassswordVerify(playerid, bool:success) + { + // success denotes verifying was successful or not + + if (success) + { + // Verified + } + else + { + // Hash doesn't match with text } } ``` @@ -104,7 +115,8 @@ If you are a sampctl user **Example** ```Pawn - main(){ + main() + { bcrypt_set_thread_limit(3); } ``` diff --git a/include/samp_bcrypt.inc b/include/samp_bcrypt.inc index ce5cc87..4643de6 100644 --- a/include/samp_bcrypt.inc +++ b/include/samp_bcrypt.inc @@ -17,67 +17,80 @@ #endif /* -bcrypt_hash(playerid, const callback[], const input[],cost,const args[] = "", {Float, _}:...) +bcrypt_hash(playerid, const callback[], const input[], cost, const args[] = "", {Float, _}:...) Params `playerid` - id of the player `callback[]` - callback to execute after hashing `input[]` - string to hash `cost` - work factor (4 - 31) - `args` - custom arguments + `args[]` - custom arguments Example ``` - main(){ - bcrypt_hash(0,"OnPassswordHash","text",BCRYPT_COST); + main() + { + bcrypt_hash(0, "OnPassswordHash", "text", BCRYPT_COST); } - forward OnPassswordHash(playerid,hashid); - public OnPassswordHash(playerid,hashid){ - //hashid is id of stored result in memory + forward OnPassswordHash(playerid, hashid); + public OnPassswordHash(playerid, hashid) + { + // hashid is id of stored result in memory } ``` */ -native bcrypt_hash(playerid, const callback[], const input[],cost,const args[] = "", {Float, _}:...); +native bcrypt_hash(playerid, const callback[], const input[], cost, const args[] = "", {Float, _}:...); + /* -bcrypt_verify(playerid,callback[],input[],hash[]) +bcrypt_verify(playerid, const callback[], const input[], const hash[], const args[] = "", {Float, _}:...) Params `playerid` - id of the player `callback[]` - callback to execute after hashing `input[]` - text to compare with hash `hash[]` - hash to compare with text - `args` - custom arguments + `args[]` - custom arguments + Example ``` - main(){ - bcrypt_verify(0,"OnPassswordVerify","text","$2y$12$lSzxFYNULh7weMGb8tf0beY1Lkb429nF.umuO/n0O.Q3U6wb1h5x. -"); + main() + { + bcrypt_verify(0, "OnPassswordVerify", "text", "$2y$12$lSzxFYNULh7weMGb8tf0beY1Lkb429nF.umuO/n0O.Q3U6wb1h5x."); } - forward OnPassswordVerify(playerid,bool:success); - public OnPassswordVerify(playerid,bool:success){ - //success denotes verifying was successful or not - if(success){ - //verfied - } else{ - //hash doesn't match with text + forward OnPassswordVerify(playerid, bool:success); + public OnPassswordVerify(playerid, bool:success) + { + // success denotes verifying was successful or not + + if (success) + { + // Verified + } + else + { + // Hash doesn't match with text } } ``` */ -native bcrypt_verify(playerid, const callback[], const input[], const hash[],const args[] = "", {Float, _}:...); +native bcrypt_verify(playerid, const callback[], const input[], const hash[], const args[] = "", {Float, _}:...); + /* -bcrypt_get_hash(dest[],size = sizeof(hash)) +bcrypt_get_hash(dest[], size = sizeof(hash)) Params `dest[]` - string to store hashed data `size` - max size of dest string + Example ``` - main(){ - bcrypt_hash(0,"OnPassswordHash","text",BCRYPT_COST); + main() + { + bcrypt_hash(0, "OnPassswordHash", "text", BCRYPT_COST); } - forward OnPassswordHash(playerid,hashid); - public OnPassswordHash(playerid,hashid){ + forward OnPassswordHash(playerid, hashid); + public OnPassswordHash(playerid, hashid) + { new dest[BCRYPT_HASH_LENGTH]; bcrypt_get_hash(dest); - printf("hash : %s",dest); + printf("hash : %s", dest); } ``` */ @@ -89,7 +102,8 @@ bcrypt_set_thread_limit(value) `value` - number of worker threads at a time Example ``` - main(){ + main() + { bcrypt_set_thread_limit(3); } ``` diff --git a/pawn-tests/test.pwn b/pawn-tests/test.pwn index 1c9b52b..d57ec59 100644 --- a/pawn-tests/test.pwn +++ b/pawn-tests/test.pwn @@ -5,45 +5,49 @@ #include "../include/samp_bcrypt.inc" -Test:TestBcryptSetNumThreads(){ +Test:TestBcryptSetNumThreads() +{ ASSERT(bcrypt_set_thread_limit(-1) == 0); ASSERT(bcrypt_set_thread_limit(0) == 0); ASSERT(bcrypt_set_thread_limit(3) == 1); } -Test:TestBcryptHash(){ - bcrypt_hash(0,"OnPassswordHash","text",12); - bcrypt_hash(0,"OnPassswordHash2","test",4); - bcrypt_hash(0,"OnPassswordHash3","test",4,"issf",69,"hello","world",10.0); - +Test:TestBcryptHash() +{ + bcrypt_hash(0, "OnPassswordHash", "text", 12); + bcrypt_hash(0, "OnPassswordHash2", "test", 4); + bcrypt_hash(0, "OnPassswordHash3", "test", 4, "issf", 69, "hello", "world", 10.0); } -Test:TestInvalidCustomArgs() { - ASSERT(bcrypt_hash(0,"WontCall","test",4,"issf",69,"world",10.0)==0); - ASSERT(bcrypt_hash(0,"WontCall","test",4,"issf",69,"world",10.0,1,1)==0); - ASSERT(bcrypt_hash(0,"WontCall","test",4,"issf",69,"world",10.0,1,1)==0); +Test:TestInvalidCustomArgs() +{ + ASSERT(bcrypt_hash(0, "WontCall", "test", 4, "issf", 69, "world", 10.0) == 0); + ASSERT(bcrypt_hash(0, "WontCall", "test", 4, "issf", 69, "world", 10.0, 1, 1) == 0); + ASSERT(bcrypt_hash(0, "WontCall", "test", 4, "issf", 69, "world", 10.0, 1, 1) == 0); - ASSERT(bcrypt_verify(0,"WontCall","test","test_hash","issf",69,"world",10.0)==0); - ASSERT(bcrypt_verify(0,"WontCall","test","test_hash","issf",69,"world",10.0,1,1)==0); - ASSERT(bcrypt_verify(0,"WontCall","test","test_hash","issf",69,"world",10.0,1,1)==0); + ASSERT(bcrypt_verify(0, "WontCall", "test", "test_hash", "issf", 69, "world", 10.0) == 0); + ASSERT(bcrypt_verify(0, "WontCall", "test", "test_hash", "issf", 69, "world", 10.0, 1, 1) == 0); + ASSERT(bcrypt_verify(0, "WontCall", "test", "test_hash", "issf", 69, "world", 10.0, 1, 1) == 0); } forward OnPassswordHash(playerid); -public OnPassswordHash(playerid){ +public OnPassswordHash(playerid) +{ printf("***OnPassswordHash"); new dest[BCRYPT_HASH_LENGTH]; bcrypt_get_hash(dest); printf("hash is %s",dest); - bcrypt_verify(playerid,"OnPassswordVerifyValid","text",dest); - bcrypt_verify(playerid,"OnPassswordVerifyInvalid","test",dest); + bcrypt_verify(playerid, "OnPassswordVerifyValid", "text", dest); + bcrypt_verify(playerid, "OnPassswordVerifyInvalid", "test", dest); } -forward OnPassswordHash3(playerid,int1,str1[],str2[],Float:float1); -public OnPassswordHash3(playerid,int1,str1[],str2[],Float:float1){ +forward OnPassswordHash3(playerid, int1, str1[], str2[], Float:float1); +public OnPassswordHash3(playerid, int1, str1[], str2[], Float:float1) +{ printf("***OnPassswordHash3"); ASSERT(int1 == 69); - new comp1 = strcmp("hello",str1); - new comp2 = strcmp("world",str2); + new comp1 = strcmp("hello", str1); + new comp2 = strcmp("world", str2); ASSERT(int1 == 69); ASSERT(comp1 == 0); @@ -51,31 +55,34 @@ public OnPassswordHash3(playerid,int1,str1[],str2[],Float:float1){ ASSERT(float1 == 10.0); new dest[250]; bcrypt_get_hash(dest); - printf("hash is %s",dest); - bcrypt_verify(playerid,"OnPassswordVerifyInvalid","text",dest); - bcrypt_verify(playerid,"OnPassswordVerifyValid","test",dest); - bcrypt_verify(playerid,"OnPassswordVerifyValidWithArgs","test",dest,"issf",69,"hello","world",10.0); + printf("hash is %s", dest); + bcrypt_verify(playerid, "OnPassswordVerifyInvalid", "text", dest); + bcrypt_verify(playerid, "OnPassswordVerifyValid", "test", dest); + bcrypt_verify(playerid, "OnPassswordVerifyValidWithArgs", "test", dest, "issf", 69, "hello", "world", 10.0); } forward OnPassswordHash2(playerid); -public OnPassswordHash2(playerid){ +public OnPassswordHash2(playerid) +{ printf("***OnPassswordHash2"); new dest[BCRYPT_HASH_LENGTH]; bcrypt_get_hash(dest); - printf("hash is %s",dest); - bcrypt_verify(playerid,"OnPassswordVerifyInvalid","text",dest); - bcrypt_verify(playerid,"OnPassswordVerifyValid","test",dest); + printf("hash is %s", dest); + bcrypt_verify(playerid, "OnPassswordVerifyInvalid", "text", dest); + bcrypt_verify(playerid, "OnPassswordVerifyValid", "test", dest); } -forward OnPassswordVerifyValid(playerid,bool:success); -public OnPassswordVerifyValid(playerid,bool:success){ +forward OnPassswordVerifyValid(playerid, bool:success); +public OnPassswordVerifyValid(playerid, bool:success) +{ printf("***OnPassswordVerifyValid"); ASSERT(success == true); print("\nPASS!"); } -forward OnPassswordVerifyValidWithArgs(playerid,bool:success,int1,str1[],str2[],Float:float1); -public OnPassswordVerifyValidWithArgs(playerid,bool:success,int1,str1[],str2[],Float:float1){ +forward OnPassswordVerifyValidWithArgs(playerid, bool:success, int1, str1[], str2[], Float:float1); +public OnPassswordVerifyValidWithArgs(playerid, bool:success, int1, str1[], str2[], Float:float1) +{ printf("***OnPassswordVerifyValidWithArgs"); ASSERT(success == true); ASSERT(int1 == 69); @@ -89,8 +96,9 @@ public OnPassswordVerifyValidWithArgs(playerid,bool:success,int1,str1[],str2[],F print("\nPASS!"); } -forward OnPassswordVerifyInvalid(playerid,bool:success); -public OnPassswordVerifyInvalid(playerid,bool:success){ +forward OnPassswordVerifyInvalid(playerid, bool:success); +public OnPassswordVerifyInvalid(playerid, bool:success) +{ printf("***OnPassswordVerifyInvalid"); ASSERT(success == false); print("\nPASS!");