Skip to content

Commit

Permalink
Merge pull request #24 from adib-yg/master
Browse files Browse the repository at this point in the history
Fix formatting
  • Loading branch information
Sreyas-Sreelal authored Feb 4, 2024
2 parents f74f1d3 + 84bf525 commit e886e3e
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 88 deletions.
64 changes: 38 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
```
Expand All @@ -104,7 +115,8 @@ If you are a sampctl user

**Example**
```Pawn
main(){
main()
{
bcrypt_set_thread_limit(3);
}
```
70 changes: 42 additions & 28 deletions include/samp_bcrypt.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
```
*/
Expand All @@ -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);
}
```
Expand Down
76 changes: 42 additions & 34 deletions pawn-tests/test.pwn
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,84 @@

#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);
ASSERT(comp2 == 0);
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);
Expand All @@ -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!");
Expand Down

0 comments on commit e886e3e

Please sign in to comment.