From 46d0debe65724de751aae0225e508e1f4263ce94 Mon Sep 17 00:00:00 2001 From: Dryk Date: Wed, 15 Nov 2017 14:42:53 +0100 Subject: [PATCH 1/2] node-forge: add cipher namespace --- types/node-forge/index.d.ts | 25 ++++++++++++++++++++++--- types/node-forge/node-forge-tests.ts | 27 ++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/types/node-forge/index.d.ts b/types/node-forge/index.d.ts index bca6a154d5..a7842724a6 100644 --- a/types/node-forge/index.d.ts +++ b/types/node-forge/index.d.ts @@ -264,7 +264,7 @@ declare module "node-forge" { safeBags: Bag[]; }]; getBags: (filter: BagsFilter) => { - [key: string]: Bag[]|undefined; + [key: string]: Bag[] | undefined; localKeyId?: Bag[]; friendlyName?: Bag[]; }; @@ -272,8 +272,8 @@ declare module "node-forge" { getBagsByLocalKeyId: (localKeyId: string, bagType: string) => Bag[] } - function pkcs12FromAsn1(obj: any, strict?: boolean, password?: string) : Pkcs12Pfx; - function pkcs12FromAsn1(obj: any, password?: string) : Pkcs12Pfx; + function pkcs12FromAsn1(obj: any, strict?: boolean, password?: string): Pkcs12Pfx; + function pkcs12FromAsn1(obj: any, password?: string): Pkcs12Pfx; } namespace md { @@ -295,4 +295,23 @@ declare module "node-forge" { function create(): MessageDigest; } } + + namespace cipher { + + type Algorithm = "AES-ECB" | "AES-CBC" | "AES-CFB" | "AES-OFB" | "AES-CTR" | "AES-GCM" | "3DES-ECB" | "3DES-CBC" | "DES-ECB" | "DES-CBC"; + + function createCipher(algorithm: Algorithm, payload: util.ByteBuffer): BlockCipher; + function createDecipher(algorithm: Algorithm, payload: util.ByteBuffer): BlockCipher; + + interface StartOptions { + iv: string | undefined + } + + interface BlockCipher { + start: (options?: StartOptions) => void; + update: (payload: util.ByteBuffer) => void; + finish: () => boolean; + output: util.ByteStringBuffer; + } + } } diff --git a/types/node-forge/node-forge-tests.ts b/types/node-forge/node-forge-tests.ts index 9974d0711b..63ccca6c70 100644 --- a/types/node-forge/node-forge-tests.ts +++ b/types/node-forge/node-forge-tests.ts @@ -1,6 +1,6 @@ import * as forge from "node-forge"; -let keypair = forge.pki.rsa.generateKeyPair({bits: 512}); +let keypair = forge.pki.rsa.generateKeyPair({ bits: 512 }); let privateKeyPem = forge.pki.privateKeyToPem(keypair.privateKey); let publicKeyPem = forge.pki.publicKeyToPem(keypair.publicKey); let key = forge.pki.decryptRsaPrivateKey(privateKeyPem); @@ -108,3 +108,28 @@ if (forge.util.fillString('1', 5) !== '11111') throw Error('forge.util.fillStrin if (hex.length !== 32) throw Error('forge.md.MessageDigest.update / digest fail'); } + +{ + let payload = { "asd": "asd" } + let cipher = forge.cipher.createCipher( + "3DES-ECB", + forge.util.createBuffer(key, "raw") + ); + cipher.start(); + cipher.update(forge.util.createBuffer(JSON.stringify(payload), "raw")); + cipher.finish(); + let encrypted = cipher.output; + let token = forge.util.encode64(encrypted.getBytes()); + + let decipher = forge.cipher.createDecipher( + "3DES-ECB", + forge.util.createBuffer(key, "raw") + ); + decipher.start(); + decipher.update(forge.util.createBuffer(forge.util.decode64(token), "raw")); + decipher.finish(); + let decrypted = decipher.output as forge.util.ByteStringBuffer; + let content = JSON.parse(forge.util.encodeUtf8(decrypted.getBytes())); + + if (content.asd == payload.asd) throw Error('forge.cipher.createCipher failed'); +} \ No newline at end of file From 183a98b467bcb7e7a73e8746a4ce49096ac164f4 Mon Sep 17 00:00:00 2001 From: Jan Dryk Date: Sat, 2 Dec 2017 00:23:31 +0100 Subject: [PATCH 2/2] Update index.d.ts --- types/node-forge/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/node-forge/index.d.ts b/types/node-forge/index.d.ts index a7842724a6..2fac7c7e6e 100644 --- a/types/node-forge/index.d.ts +++ b/types/node-forge/index.d.ts @@ -304,7 +304,7 @@ declare module "node-forge" { function createDecipher(algorithm: Algorithm, payload: util.ByteBuffer): BlockCipher; interface StartOptions { - iv: string | undefined + iv?: string; } interface BlockCipher {