Skip to content
This repository was archived by the owner on Dec 30, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 48 additions & 47 deletions src/ios/ScryptPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,60 @@ @implementation ScryptPlugin

- (void)scrypt:(CDVInvokedUrlCommand*)command
{

int i, success;
size_t saltLength;
const uint8_t *parsedSalt;
uint8_t *buffer = NULL;
const char* passphrase = [[command argumentAtIndex:0] UTF8String];
id salt = [command argumentAtIndex:1];

if ([salt isKindOfClass:[NSString class]]) {
parsedSalt = (const uint8_t *)[salt UTF8String];
saltLength = (size_t) [salt length];
} else if ([salt isKindOfClass:[NSArray class]]) {
saltLength = (int) [salt count];
buffer = malloc(sizeof(uint8_t) * saltLength);

for (i = 0; i < saltLength; ++i) {
buffer[i] = (uint8_t)[[salt objectAtIndex:i] integerValue];
[self.commandDelegate runInBackground:^{
int i, success;
size_t saltLength;
const uint8_t *parsedSalt;
uint8_t *buffer = NULL;
const char* passphrase = [[command argumentAtIndex:0] UTF8String];
id salt = [command argumentAtIndex:1];

if ([salt isKindOfClass:[NSString class]]) {
parsedSalt = (const uint8_t *)[salt UTF8String];
saltLength = (size_t) [salt length];
} else if ([salt isKindOfClass:[NSArray class]]) {
saltLength = (int) [salt count];
buffer = malloc(sizeof(uint8_t) * saltLength);

for (i = 0; i < saltLength; ++i) {
buffer[i] = (uint8_t)[[salt objectAtIndex:i] integerValue];
}
parsedSalt = buffer;
}
parsedSalt = buffer;
}

// Parse options
NSMutableDictionary* options = [command.arguments objectAtIndex:2];
uint64_t N = [options[@"N"] unsignedLongValue] ?: SCRYPT_N;
uint32_t r = [options[@"r"] unsignedShortValue] ?: SCRYPT_r;
uint32_t p = [options[@"p"] unsignedShortValue] ?: SCRYPT_p;
uint32_t dkLen = [options[@"dkLen"] unsignedShortValue] ?: 32;

uint8_t hashbuf[dkLen];
self.callbackId = command.callbackId;

@try {
success = libscrypt_scrypt((uint8_t *)passphrase, strlen(passphrase), parsedSalt, saltLength, N, r, p, hashbuf, dkLen);
}
@catch (NSException * e) {
[self failWithMessage: [NSString stringWithFormat:@"%@", e] withError: nil];
}
// Parse options
NSMutableDictionary* options = [command.arguments objectAtIndex:2];
uint64_t N = [options[@"N"] unsignedLongValue] ?: SCRYPT_N;
uint32_t r = [options[@"r"] unsignedShortValue] ?: SCRYPT_r;
uint32_t p = [options[@"p"] unsignedShortValue] ?: SCRYPT_p;
uint32_t dkLen = [options[@"dkLen"] unsignedShortValue] ?: 32;

uint8_t hashbuf[dkLen];
self.callbackId = command.callbackId;
@try {
success = libscrypt_scrypt((uint8_t *)passphrase, strlen(passphrase), parsedSalt, saltLength, N, r, p, hashbuf, dkLen);
}
@catch (NSException * e) {
[self failWithMessage: [NSString stringWithFormat:@"%@", e] withError: nil];
}

if (success!=0) {
[self failWithMessage: @"Failure in scrypt" withError: nil];
}
if (success!=0) {
[self failWithMessage: @"Failure in scrypt" withError: nil];
}


// Hexify
NSMutableString *hexResult = [NSMutableString stringWithCapacity:dkLen * 2];
for(i = 0;i < dkLen; i++ )
{
[hexResult appendFormat:@"%02x", hashbuf[i]];
}
NSString *result = [NSString stringWithString: hexResult];
[self successWithMessage: result];
// Hexify
NSMutableString *hexResult = [NSMutableString stringWithCapacity:dkLen * 2];
for(i = 0;i < dkLen; i++ )
{
[hexResult appendFormat:@"%02x", hashbuf[i]];
}
NSString *result = [NSString stringWithString: hexResult];
[self successWithMessage: result];

free(buffer);
free(buffer);
}];
}

-(void)successWithMessage:(NSString *)message
Expand Down
40 changes: 37 additions & 3 deletions www/scrypt.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,48 @@
var scrypt = function(successCallback, errorCallback, message, salt, options) {
if (typeof errorCallback != "function") {
console.log("ScryptPlugin.scrypt failure: failure parameter not a function");
console.error("ScryptPlugin.scrypt failure: failure parameter not a function");
return;
}

if (typeof successCallback != "function") {
console.log("ScryptPlugin.scrypt failure: success callback parameter must be a function");
console.error("ScryptPlugin.scrypt failure: success callback parameter must be a function");
return;
}

if (typeof message != 'string'){
console.error('ScryptPlugin.scrypt failure: message must be a string');
return;
}
if (!(typeof salt == 'string' || Array.isArray(salt))){
console.error('ScryptPlugin.scrypt failure: salt must either be a string or an array of bytes');
return;
}
if (Array.isArray(salt)){
for (var i = 0; i < salt.length; i++){
if (!(typeof salt[i] == 'number' && salt[i] >= 0 && salt[i] <= 255 && Math.floor(salt[i]) == salt[i])){
console.error('ScryptPlugin.scrypt failure: when salt is an array, it must only contain bytes');
return;
}
}
}

options = options || {};
if (options.N && !(typeof options.N == 'number' && Math.floor(options.N) == options.N && options.N > 0)){
console.error('ScryptPlugin.scrypt failure: when defined, options.N must be a strictly positive integer number');
return;
}
if (options.dkLen && !(typeof options.dkLen == 'number' && Math.floor(options.dkLen) == options.dkLen && options.dkLen > 0)){
console.error('ScryptPlugin.scrypt failure: when defined, options.dkLen must be a strictly positive integer number');
return;
}
if (options.r && !(typeof options.r == 'number' && Math.floor(options.r) == options.r && options.r > 0)){
console.error('ScryptPlugin.scrypt failure: when defined, options.r must be a strictly positive integer number');
return;
}
if (options.p && !(typeof options.p == 'number' && Math.floor(options.p) == options.p && options.p > 0)){
console.error('ScryptPlugin.scrypt failure: when defined, options.p must be a strictly positive integer number');
return;
}
cordova.exec(successCallback, errorCallback, "ScryptPlugin", "scrypt", [message, salt, options]);
};

Expand All @@ -22,4 +56,4 @@ if (!window.plugins.scrypt) {

if (typeof module != 'undefined' && module.exports) {
module.exports = scrypt;
}
}