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 OperationError from "../errors/OperationError.mjs" ;
2018-05-17 17:11:34 +02:00
import JSSHA3 from "js-sha3" ;
/ * *
* Shake operation
* /
class Shake extends Operation {
/ * *
* Shake constructor
* /
constructor ( ) {
super ( ) ;
this . name = "Shake" ;
2018-12-26 00:58:00 +01:00
this . module = "Crypto" ;
2018-05-17 17:11:34 +02:00
this . description = "Shake is an Extendable Output Function (XOF) of the SHA-3 hash algorithm, part of the Keccak family, allowing for variable output length/size." ;
2018-08-21 20:07:13 +02:00
this . infoURL = "https://wikipedia.org/wiki/SHA-3#Instances" ;
2018-05-17 17:11:34 +02:00
this . inputType = "ArrayBuffer" ;
this . outputType = "string" ;
this . args = [
{
"name" : "Capacity" ,
"type" : "option" ,
"value" : [ "256" , "128" ]
} ,
{
"name" : "Size" ,
"type" : "number" ,
"value" : 512
}
] ;
}
/ * *
* @ param { ArrayBuffer } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
const capacity = parseInt ( args [ 0 ] , 10 ) ,
size = args [ 1 ] ;
let algo ;
if ( size < 0 )
throw new OperationError ( "Size must be greater than 0" ) ;
switch ( capacity ) {
case 128 :
algo = JSSHA3 . shake128 ;
break ;
case 256 :
algo = JSSHA3 . shake256 ;
break ;
default :
throw new OperationError ( "Invalid size" ) ;
}
return algo ( input , size ) ;
}
}
export default Shake ;