2018-05-17 17:11:34 +02:00
/ * *
* @ author n1474335 [ n1474335 @ gmail . com ]
* @ copyright Crown Copyright 2016
* @ license Apache - 2.0
* /
2019-07-09 13:23:59 +02:00
import Operation from "../Operation.mjs" ;
import Utils from "../Utils.mjs" ;
import OperationError from "../errors/OperationError.mjs" ;
2018-05-17 17:11:34 +02:00
import scryptsy from "scryptsy" ;
2019-07-09 13:23:59 +02:00
import { isWorkerEnvironment } from "../Utils.mjs" ;
2018-05-17 17:11:34 +02:00
/ * *
* Scrypt operation
* /
class Scrypt extends Operation {
/ * *
* Scrypt constructor
* /
constructor ( ) {
super ( ) ;
this . name = "Scrypt" ;
2018-12-26 00:58:00 +01:00
this . module = "Crypto" ;
2018-05-17 17:11:34 +02:00
this . description = "scrypt is a password-based key derivation function (PBKDF) created by Colin Percival. The algorithm was specifically designed to make it costly to perform large-scale custom hardware attacks by requiring large amounts of memory. In 2016, the scrypt algorithm was published by IETF as RFC 7914.<br><br>Enter the password in the input to generate its hash." ;
2018-08-21 20:07:13 +02:00
this . infoURL = "https://wikipedia.org/wiki/Scrypt" ;
2018-05-17 17:11:34 +02:00
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [
{
"name" : "Salt" ,
"type" : "toggleString" ,
"value" : "" ,
"toggleValues" : [ "Hex" , "Base64" , "UTF8" , "Latin1" ]
} ,
{
"name" : "Iterations (N)" ,
"type" : "number" ,
"value" : 16384
} ,
{
"name" : "Memory factor (r)" ,
"type" : "number" ,
"value" : 8
} ,
{
"name" : "Parallelization factor (p)" ,
"type" : "number" ,
"value" : 1
} ,
{
"name" : "Key length" ,
"type" : "number" ,
"value" : 64
}
] ;
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
2018-10-22 03:10:49 +02:00
const salt = Buffer . from ( Utils . convertToByteArray ( args [ 0 ] . string || "" , args [ 0 ] . option ) ) ,
2018-05-17 17:11:34 +02:00
iterations = args [ 1 ] ,
memFactor = args [ 2 ] ,
parallelFactor = args [ 3 ] ,
keyLength = args [ 4 ] ;
try {
const data = scryptsy (
input , salt , iterations , memFactor , parallelFactor , keyLength ,
p => {
// Progress callback
2019-07-05 11:17:52 +02:00
if ( isWorkerEnvironment ( ) )
2018-05-17 17:11:34 +02:00
self . sendStatusMessage ( ` Progress: ${ p . percent . toFixed ( 0 ) } % ` ) ;
}
) ;
return data . toString ( "hex" ) ;
} catch ( err ) {
throw new OperationError ( "Error: " + err . toString ( ) ) ;
}
}
}
export default Scrypt ;