From 040be0b1cce5f6addbf3ed9a2923e488919bb35a Mon Sep 17 00:00:00 2001 From: Thermod Date: Wed, 26 Feb 2020 04:49:07 +0800 Subject: [PATCH] Add types for sm-crypto (#42594) * Add types for sm-crypto * fix Definitions comment in index.d.ts --- types/sm-crypto/index.d.ts | 48 ++++++++++++++++++++++ types/sm-crypto/sm-crypto-tests.ts | 66 ++++++++++++++++++++++++++++++ types/sm-crypto/tsconfig.json | 23 +++++++++++ types/sm-crypto/tslint.json | 1 + 4 files changed, 138 insertions(+) create mode 100644 types/sm-crypto/index.d.ts create mode 100644 types/sm-crypto/sm-crypto-tests.ts create mode 100644 types/sm-crypto/tsconfig.json create mode 100644 types/sm-crypto/tslint.json diff --git a/types/sm-crypto/index.d.ts b/types/sm-crypto/index.d.ts new file mode 100644 index 0000000000..94bf54d095 --- /dev/null +++ b/types/sm-crypto/index.d.ts @@ -0,0 +1,48 @@ +// Type definitions for sm-crypto 0.1 +// Project: https://github.com/JuneAndGreen/sm-crypto#readme +// Definitions by: Thermod +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import BigInteger = require('bigi'); + +export interface KeyPairHex { + privateKey: string; + publicKey: string; +} + +export interface KeyPairPoint extends KeyPairHex { + k: BigInteger; + x1: BigInteger; +} + +/** + * Cipher Mode + * - `0`:C1C2C3 + * - `1`:C1C3C2 + */ +export type CipherMode = 0 | 1; + +export namespace sm2 { + function generateKeyPairHex(): KeyPairHex; + function doEncrypt(msg: string, publicKey: string, cipherMode?: CipherMode): string; + function doDecrypt(encryptData: string, privateKey: string, cipherMode?: CipherMode): string; + function doSignature(msg: string, privateKey: string, options?: { + pointPool?: KeyPairPoint[] + der?: boolean + hash?: boolean + publicKey?: string + }): string; + function doVerifySignature(msg: string, signHex: string, publicKey: string, options?: { + der?: boolean + hash?: boolean + publicKey?: string + }): boolean; + function getPoint(): KeyPairPoint; +} + +export function sm3(str: string): string; + +export namespace sm4 { + function encrypt(inArray: number[], key: number[]): number[]; + function decrypt(inArray: number[], key: number[]): number[]; +} diff --git a/types/sm-crypto/sm-crypto-tests.ts b/types/sm-crypto/sm-crypto-tests.ts new file mode 100644 index 0000000000..3f72e8ba10 --- /dev/null +++ b/types/sm-crypto/sm-crypto-tests.ts @@ -0,0 +1,66 @@ +import { sm2, sm3, sm4 } from 'sm-crypto'; + +const message = "Hello World!"; + +// get key pair +const keypair = sm2.generateKeyPairHex(); + +const publicKey = keypair.publicKey; +const privateKey = keypair.privateKey; + +// encrypt & decrypt +const encryptData = sm2.doEncrypt(message, publicKey, 1); // encrypt result +const decryptData = sm2.doDecrypt(encryptData, privateKey, 1); // decrypt result + +// signature +// pure sign + generate elliptic curve points +const sigValueHex = sm2.doSignature(message, privateKey); // sign +const verifyResult = sm2.doVerifySignature(message, sigValueHex, publicKey); // verify sign result + +// pure sign +const sigValueHex2 = sm2.doSignature(message, privateKey, { + // speed up sign by passing in elliptic curve points that have been generated in advance + pointPool: [sm2.getPoint(), sm2.getPoint(), sm2.getPoint(), sm2.getPoint()], +}); // sign +const verifyResult2 = sm2.doVerifySignature(message, sigValueHex2, publicKey); // verify sign result + +// pure sign + generate elliptic curve points + der encoding +const sigValueHex3 = sm2.doSignature(message, privateKey, { + der: true, +}); // sign +const verifyResult3 = sm2.doVerifySignature(message, sigValueHex3, publicKey, { + der: true, +}); // verify sign result + +// pure sign + generate elliptic curve points + sm3 +const sigValueHex4 = sm2.doSignature(message, privateKey, { + hash: true, +}); // sign +const verifyResult4 = sm2.doVerifySignature(message, sigValueHex4, publicKey, { + hash: true, +}); // verify sign result + +// pure sign + generate elliptic curve points + sm3 without deriving the public key +const sigValueHex5 = sm2.doSignature(message, privateKey, { + hash: true, + publicKey, // if we passing in public key, we can skip deriving the public key in sm3, it will be more faster than previous method +}); +const verifyResult5 = sm2.doVerifySignature(message, sigValueHex5, publicKey, { + hash: true, + publicKey, +}); + +// obtain a elliptic curve point +const poin = sm2.getPoint(); // can using in sm2 sign + +// sm3 +const hashData = sm3('abc'); + +// sm4 +const key = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10]; + +// encrypt +const sm4EncryptData = sm4.encrypt([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10], key); + +// decrypt +const sm4DecryptData = sm4.decrypt([0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e, 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46], key); diff --git a/types/sm-crypto/tsconfig.json b/types/sm-crypto/tsconfig.json new file mode 100644 index 0000000000..5b078209d9 --- /dev/null +++ b/types/sm-crypto/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "sm-crypto-tests.ts" + ] +} diff --git a/types/sm-crypto/tslint.json b/types/sm-crypto/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/sm-crypto/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }