mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-30 18:43:21 +08:00
Merge pull request #21536 from uxsoft/node-forge--add-cipher-namespace
node-forge: add cipher namespace
This commit is contained in:
25
types/node-forge/index.d.ts
vendored
25
types/node-forge/index.d.ts
vendored
@@ -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;
|
||||
}
|
||||
|
||||
interface BlockCipher {
|
||||
start: (options?: StartOptions) => void;
|
||||
update: (payload: util.ByteBuffer) => void;
|
||||
finish: () => boolean;
|
||||
output: util.ByteStringBuffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
Reference in New Issue
Block a user