2018-05-23 17:54:12 +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" ;
2019-09-27 00:02:03 +02:00
import forge from "node-forge/dist/forge.min.js" ;
2019-07-09 13:23:59 +02:00
import OperationError from "../errors/OperationError.mjs" ;
2019-09-27 00:02:03 +02:00
import { Blowfish } from "../lib/Blowfish.mjs" ;
2018-05-23 17:54:12 +02:00
/ * *
* Blowfish Encrypt operation
* /
class BlowfishEncrypt extends Operation {
/ * *
* BlowfishEncrypt constructor
* /
constructor ( ) {
super ( ) ;
this . name = "Blowfish Encrypt" ;
this . module = "Ciphers" ;
this . description = "Blowfish is a symmetric-key block cipher designed in 1993 by Bruce Schneier and included in a large number of cipher suites and encryption products. AES now receives more attention.<br><br><b>IV:</b> The Initialization Vector should be 8 bytes long. If not entered, it will default to 8 null bytes." ;
2018-08-21 20:07:13 +02:00
this . infoURL = "https://wikipedia.org/wiki/Blowfish_(cipher)" ;
2018-05-23 17:54:12 +02:00
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [
{
"name" : "Key" ,
"type" : "toggleString" ,
"value" : "" ,
"toggleValues" : [ "Hex" , "UTF8" , "Latin1" , "Base64" ]
} ,
{
"name" : "IV" ,
"type" : "toggleString" ,
"value" : "" ,
"toggleValues" : [ "Hex" , "UTF8" , "Latin1" , "Base64" ]
} ,
{
"name" : "Mode" ,
"type" : "option" ,
2019-09-27 00:02:03 +02:00
"value" : [ "CBC" , "CFB" , "OFB" , "CTR" , "ECB" ]
2018-05-23 17:54:12 +02:00
} ,
{
"name" : "Input" ,
"type" : "option" ,
"value" : [ "Raw" , "Hex" ]
} ,
{
"name" : "Output" ,
"type" : "option" ,
"value" : [ "Hex" , "Base64" , "Raw" ]
}
] ;
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
const key = Utils . convertToByteString ( args [ 0 ] . string , args [ 0 ] . option ) ,
2019-09-27 00:02:03 +02:00
iv = Utils . convertToByteString ( args [ 1 ] . string , args [ 1 ] . option ) ,
mode = args [ 2 ] ,
inputType = args [ 3 ] ,
outputType = args [ 4 ] ;
2018-05-23 17:54:12 +02:00
2018-05-27 17:13:18 +02:00
if ( key . length === 0 ) throw new OperationError ( "Enter a key" ) ;
2018-05-23 17:54:12 +02:00
input = Utils . convertToByteString ( input , inputType ) ;
2019-09-27 00:02:03 +02:00
const cipher = Blowfish . createCipher ( key , mode ) ;
cipher . start ( { iv : iv } ) ;
cipher . update ( forge . util . createBuffer ( input ) ) ;
cipher . finish ( ) ;
2018-05-23 17:54:12 +02:00
2019-09-27 00:02:03 +02:00
if ( outputType === "Hex" ) {
return cipher . output . toHex ( ) ;
} else {
return cipher . output . getBytes ( ) ;
}
2018-05-23 17:54:12 +02:00
}
}
export default BlowfishEncrypt ;