import * as chai from "chai" ; import chaiAsPromised from "chai-as-promised" ; import * as fs from "fs" ; import * as path from "path" ; import { decryptArrayBuffer , decryptBase32ToString , encryptArrayBuffer , encryptStringToBase32 , encryptStringToBase64url , } from "../src/encrypt" ; import { base64ToBase64url , bufferToArrayBuffer } from "../src/misc" ; chai . use ( chaiAsPromised ) ; const expect = chai . expect ; describe ( "Encryption tests" , ( ) => { beforeEach ( function ( ) { global . window = { crypto : require ( "crypto" ) . webcrypto , } as any ; } ) ; it ( "should encrypt string" , async ( ) => { const k = "dkjdhkfhdkjgsdklxxd" ; const password = "hey" ; expect ( await encryptStringToBase32 ( k , password ) ) . to . not . equal ( k ) ; } ) ; it ( "should raise error using different password" , async ( ) => { const k = "secret text" ; const password = "hey" ; const password2 = "hey2" ; const enc = await encryptStringToBase32 ( k , password ) ; await expect ( decryptBase32ToString ( enc , password2 ) ) . to . be . rejected ; } ) ; it ( "should encrypt and decrypt string and get the same result returned" , async ( ) => { const k = "jfkkjkjbce7983ycdeknkkjckooAIUHIDIBIE((*BII)njD/d/dd/d/sjxhux" ; const password = "hfiuibec989###oiu982bj1`" ; const enc = await encryptStringToBase32 ( k , password ) ; const dec = await decryptBase32ToString ( enc , password ) ; expect ( dec ) . equal ( k ) ; } ) ; it ( "should encrypt text file and get the same result as openssl" , async ( ) => { const fileContent = ( await fs . readFileSync ( path . join ( __dirname , "static_assets" , "sometext.txt" ) ) ) . toString ( "utf-8" ) ; const password = "somepassword" ; const saltHex = "8302F586FAB491EC" ; const enc = await encryptStringToBase64url ( fileContent , password , undefined , saltHex ) ; const opensslBase64Res = "U2FsdGVkX1+DAvWG+rSR7BPXMnlvSSVGMdjsx7kE1CTH+28P+yAZRdDGgFWMGkMd" ; const opensslBase64urlRes = base64ToBase64url ( opensslBase64Res ) ; expect ( enc ) . equal ( opensslBase64urlRes ) ; } ) ; it ( "should encrypt binary file and get the same result as openssl" , async ( ) => { const testFolder = path . join ( __dirname , "static_assets" , "mona_lisa" ) ; const testFileName = "1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg" ; const fileArrBuf = bufferToArrayBuffer ( await fs . readFileSync ( path . join ( testFolder , testFileName ) ) ) ; const password = "somepassword" ; const saltHex = "8302F586FAB491EC" ; const enc = await encryptArrayBuffer ( fileArrBuf , password , undefined , saltHex ) ; const opensslArrBuf = bufferToArrayBuffer ( await fs . readFileSync ( path . join ( testFolder , testFileName + ".enc" ) ) ) ; expect ( Buffer . from ( enc ) . equals ( Buffer . from ( opensslArrBuf ) ) ) . to . be . true ; } ) ; it ( "should descypt binary file and get the same result as openssl" , async ( ) => { const testFolder = path . join ( __dirname , "static_assets" , "mona_lisa" ) ; const testFileName = "1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg" ; const fileArrBuf = bufferToArrayBuffer ( await fs . readFileSync ( path . join ( testFolder , testFileName + ".enc" ) ) ) ; const password = "somepassword" ; const dec = await decryptArrayBuffer ( fileArrBuf , password ) ; const opensslArrBuf = bufferToArrayBuffer ( await fs . readFileSync ( path . join ( testFolder , testFileName ) ) ) ; expect ( Buffer . from ( dec ) . equals ( Buffer . from ( opensslArrBuf ) ) ) . to . be . true ; } ) ; } ) ;