@@ -19,6 +19,7 @@ import { Convert as convert } from '../format/Convert';
1919import { KeyPair } from './KeyPair' ;
2020import { SignSchema } from './SignSchema' ;
2121import * as utility from './Utilities' ;
22+
2223// tslint:disable-next-line: no-var-requires
2324const CryptoJS = require ( 'crypto-js' ) ;
2425export class Crypto {
@@ -223,7 +224,12 @@ export class Crypto {
223224 * @param {SignSchema } signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult))
224225 * @return {string } - The encoded message
225226 */
226- public static _encode = ( senderPriv , recipientPub , msg , iv , salt , signSchema : SignSchema ) => {
227+ public static _encode = ( senderPriv : string ,
228+ recipientPub : string ,
229+ msg : string ,
230+ iv : Uint8Array ,
231+ salt : Uint8Array ,
232+ signSchema : SignSchema ) : string => {
227233 // Errors
228234 if ( ! senderPriv || ! recipientPub || ! msg || ! iv || ! salt ) { throw new Error ( 'Missing argument !' ) ; }
229235 // Processing
@@ -233,7 +239,7 @@ export class Crypto {
233239 const encIv = {
234240 iv : utility . ua2words ( iv , 16 ) ,
235241 } ;
236- const encrypted = CryptoJS . AES . encrypt ( CryptoJS . enc . Hex . parse ( convert . utf8ToHex ( msg ) ) , encKey , encIv ) ;
242+ const encrypted = CryptoJS . AES . encrypt ( CryptoJS . enc . Hex . parse ( msg ) , encKey , encIv ) ;
237243 // Result
238244 const result = convert . uint8ToHex ( salt ) + convert . uint8ToHex ( iv ) + CryptoJS . enc . Hex . stringify ( encrypted . ciphertext ) ;
239245 return result ;
@@ -246,15 +252,20 @@ export class Crypto {
246252 * @param {string } recipientPub - A recipient public key
247253 * @param {string } msg - A text message
248254 * @param {SignSchema } signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult))
255+ * @param {boolean } isHexString - Is payload string a hexadecimal string (default = false)
249256 * @return {string } - The encoded message
250257 */
251- public static encode = ( senderPriv , recipientPub , msg , signSchema : SignSchema ) => {
258+ public static encode = ( senderPriv : string ,
259+ recipientPub : string ,
260+ msg : string ,
261+ signSchema : SignSchema ,
262+ isHexString : boolean = false ) : string => {
252263 // Errors
253264 if ( ! senderPriv || ! recipientPub || ! msg ) { throw new Error ( 'Missing argument !' ) ; }
254265 // Processing
255266 const iv = Crypto . randomBytes ( 16 ) ;
256267 const salt = Crypto . randomBytes ( 32 ) ;
257- const encoded = Crypto . _encode ( senderPriv , recipientPub , msg , iv , salt , signSchema ) ;
268+ const encoded = Crypto . _encode ( senderPriv , recipientPub , isHexString ? msg : convert . utf8ToHex ( msg ) , iv , salt , signSchema ) ;
258269 // Result
259270 return encoded ;
260271 }
@@ -264,11 +275,18 @@ export class Crypto {
264275 *
265276 * @param {string } recipientPrivate - A recipient private key
266277 * @param {string } senderPublic - A sender public key
267- * @param {Uint8Array } _payload - An encrypted message payload in bytes
278+ * @param {Uint8Array } payload - An encrypted message payload in bytes
279+ * @param {Uint8Array } iv - 16-byte AES initialization vector
280+ * @param {Uint8Array } salt - 32-byte salt
268281 * @param {SignSchema } signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult))
269282 * @return {string } - The decoded payload as hex
270283 */
271- public static _decode = ( recipientPrivate , senderPublic , payload , iv , salt , signSchema : SignSchema ) => {
284+ public static _decode = ( recipientPrivate : string ,
285+ senderPublic : string ,
286+ payload : Uint8Array ,
287+ iv : Uint8Array ,
288+ salt : Uint8Array ,
289+ signSchema : SignSchema ) : string => {
272290 // Error
273291 if ( ! recipientPrivate || ! senderPublic || ! payload ) { throw new Error ( 'Missing argument !' ) ; }
274292 // Processing
@@ -292,10 +310,13 @@ export class Crypto {
292310 * @param {string } recipientPrivate - A recipient private key
293311 * @param {string } senderPublic - A sender public key
294312 * @param {string } payload - An encrypted message payload
295- * @param {SignSchema } signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult))
313+ * @param {SignSchema } signSchema - The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult))
296314 * @return {string } - The decoded payload as hex
297315 */
298- public static decode = ( recipientPrivate , senderPublic , payload , signSchema : SignSchema ) => {
316+ public static decode = ( recipientPrivate : string ,
317+ senderPublic : string ,
318+ payload : string ,
319+ signSchema : SignSchema ) : string => {
299320 // Error
300321 if ( ! recipientPrivate || ! senderPublic || ! payload ) { throw new Error ( 'Missing argument !' ) ; }
301322 // Processing
0 commit comments