2019-07-03 17:24:11 +01:00
/ * *
* @ author n1474335 [ n1474335 @ gmail . com ]
* @ copyright Crown Copyright 2019
* @ license Apache - 2.0
* /
2019-07-09 12:23:59 +01:00
import Operation from "../Operation.mjs" ;
import OperationError from "../errors/OperationError.mjs" ;
import GostDigest from "../vendor/gost/gostDigest.mjs" ;
2023-07-14 18:37:02 +01:00
import { toHexFast } from "../lib/Hex.mjs" ;
2019-07-03 17:24:11 +01:00
/ * *
* GOST hash operation
* /
class GOSTHash extends Operation {
/ * *
* GOSTHash constructor
* /
constructor ( ) {
super ( ) ;
2023-07-14 18:37:02 +01:00
this . name = "GOST Hash" ;
2019-07-03 17:24:11 +01:00
this . module = "Hashing" ;
this . description = "The GOST hash function, defined in the standards GOST R 34.11-94 and GOST 34.311-95 is a 256-bit cryptographic hash function. It was initially defined in the Russian national standard GOST R 34.11-94 <i>Information Technology – Cryptographic Information Security – Hash Function</i>. The equivalent standard used by other member-states of the CIS is GOST 34.311-95.<br><br>This function must not be confused with a different Streebog hash function, which is defined in the new revision of the standard GOST R 34.11-2012.<br><br>The GOST hash function is based on the GOST block cipher." ;
this . infoURL = "https://wikipedia.org/wiki/GOST_(hash_function)" ;
this . inputType = "ArrayBuffer" ;
this . outputType = "string" ;
this . args = [
{
2023-07-14 18:37:02 +01:00
name : "Algorithm" ,
type : "argSelector" ,
value : [
{
name : "GOST 28147 (1994)" ,
off : [ 1 ] ,
on : [ 2 ]
} ,
{
name : "GOST R 34.11 (Streebog, 2012)" ,
on : [ 1 ] ,
off : [ 2 ]
}
2019-07-03 17:24:11 +01:00
]
2023-07-14 18:37:02 +01:00
} ,
{
name : "Digest length" ,
type : "option" ,
value : [ "256" , "512" ]
} ,
{
name : "sBox" ,
type : "option" ,
value : [ "E-TEST" , "E-A" , "E-B" , "E-C" , "E-D" , "E-SC" , "E-Z" , "D-TEST" , "D-A" , "D-SC" ]
2019-07-03 17:24:11 +01:00
}
] ;
}
/ * *
* @ param { ArrayBuffer } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
2023-07-14 18:37:02 +01:00
const [ version , length , sBox ] = args ;
const versionNum = version === "GOST 28147 (1994)" ? 1994 : 2012 ;
const algorithm = {
name : versionNum === 1994 ? "GOST 28147" : "GOST R 34.10" ,
version : versionNum ,
mode : "HASH"
} ;
if ( versionNum === 1994 ) {
algorithm . sBox = sBox ;
} else {
algorithm . length = parseInt ( length , 10 ) ;
}
2019-07-03 17:24:11 +01:00
try {
2023-07-14 18:37:02 +01:00
const gostDigest = new GostDigest ( algorithm ) ;
2019-07-03 17:24:11 +01:00
return toHexFast ( gostDigest . digest ( input ) ) ;
} catch ( err ) {
throw new OperationError ( err ) ;
}
}
}
export default GOSTHash ;