Skip to content

Commit

Permalink
Add support for libsodium, added Password plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabordemooij committed Mar 10, 2018
1 parent a8ba462 commit 1c5f814
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
Empty file added mods/password/.gitkeep
Empty file.
61 changes: 61 additions & 0 deletions plugins/crypt/crypt.c
@@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sodium.h>
#include "../../citrine.h"


/**
* [Hash] new: [String]
*
* Creates a new hash from the string.
*/
ctr_object* ctr_hash_new(ctr_object* myself, ctr_argument* argumentList) {
char hashed_password[crypto_pwhash_STRBYTES];
ctr_object* cryptInstance = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT);
cryptInstance->link = myself;
ctr_object* value = ctr_build_string( (char* ) &hashed_password, crypto_pwhash_STRBYTES );
ctr_internal_object_add_property( cryptInstance, ctr_build_string_from_cstring("value"), value, CTR_CATEGORY_PRIVATE_PROPERTY );
ctr_object* password = ctr_internal_cast2string( argumentList->object );
if (crypto_pwhash_str(value->value.svalue->value, password->value.svalue->value, password->value.svalue->vlen,
crypto_pwhash_OPSLIMIT_MODERATE, crypto_pwhash_MEMLIMIT_MODERATE) != 0) {
CtrStdFlow = ctr_build_string_from_cstring("Unable to encrypt password.");
}
return cryptInstance;
}

ctr_object* ctr_password_verify(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* value = ctr_internal_object_find_property( myself, ctr_build_string_from_cstring("value"), CTR_CATEGORY_PRIVATE_PROPERTY );
if (value == NULL) value = ctr_build_empty_string();
ctr_object* compare = ctr_internal_cast2string( argumentList->object );
if (crypto_pwhash_str_verify(value->value.svalue->value, compare->value.svalue->value, compare->value.svalue->vlen) != 0) {
return ctr_build_bool(0);
}
return ctr_build_bool(1);
}

ctr_object* ctr_password_to_string(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* answer = ctr_internal_object_find_property( myself, ctr_build_string_from_cstring("value"), CTR_CATEGORY_PRIVATE_PROPERTY );
if (answer == NULL) return CtrStdNil;
return answer;
}

ctr_object* ctr_password_from_hash(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* cryptInstance = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT);
cryptInstance->link = myself;
ctr_internal_object_set_property( cryptInstance, ctr_build_string_from_cstring("value") , argumentList->object, CTR_CATEGORY_PRIVATE_PROPERTY );
return cryptInstance;
}


void begin(){
if (sodium_init() >= 0) {
ctr_object* cryptObject = ctr_internal_create_object( CTR_OBJECT_TYPE_OTOBJECT );
cryptObject->link = CtrStdObject;
ctr_internal_create_func(cryptObject, ctr_build_string_from_cstring( "new:" ), &ctr_hash_new );
ctr_internal_create_func(cryptObject, ctr_build_string_from_cstring( "=" ), &ctr_password_verify );
ctr_internal_create_func(cryptObject, ctr_build_string_from_cstring( "toString" ), &ctr_password_to_string );
ctr_internal_create_func(cryptObject, ctr_build_string_from_cstring( "fromHash:" ), &ctr_password_from_hash );
ctr_internal_object_add_property(CtrStdWorld, ctr_build_string_from_cstring( "Password" ), cryptObject, CTR_CATEGORY_PUBLIC_PROPERTY);
}
}
11 changes: 10 additions & 1 deletion runtests.sh
Expand Up @@ -62,7 +62,16 @@ cd ..
cd ..
cp plugins/pg/libctrpg.so mods/pg/libctrpg.so


#sodium
rm plugins/crypt/libctrpassword.so
rm plugins/crypt/crypt.o
rm mods/password/libctrpassword.so
cd plugins/crypt;
gcc -c crypt.c -Wall -I/usr/local/include/ -Werror -fpic -o crypt.o
gcc -shared -o libctrpassword.so crypt.o -L/usr/local/lib/ -lsodium
cd ..
cd ..
cp plugins/crypt/libctrpassword.so mods/password/libctrpassword.so

make clean;
./mk.sh
Expand Down
11 changes: 11 additions & 0 deletions tests/test0319.ctr
@@ -0,0 +1,11 @@

☞ myPassword := Password new: 'hello world'.
✎ write: (myPassword = 'hello world'), brk.

☞ myPassword := Password fromHash: '$argon2i$v=19$m=131072,t=6,p=1$MuY2Gk15bL4HKBAre7O0Sg$DGJHey/NZ4q7m4f43C/gCStpJvPW/P/RXj9YD448Iv8'.
✎ write: myPassword, brk.
✎ write: (myPassword = 'hello world'), brk.

☞ myPassword := Password fromHash: 'xxx'.
✎ write: (myPassword = 'hello world'), brk.

4 changes: 4 additions & 0 deletions tests/test0319.exp
@@ -0,0 +1,4 @@
True
$argon2i$v=19$m=131072,t=6,p=1$MuY2Gk15bL4HKBAre7O0Sg$DGJHey/NZ4q7m4f43C/gCStpJvPW/P/RXj9YD448Iv8
True
False

0 comments on commit 1c5f814

Please sign in to comment.