Add types for sm-crypto (#42594)

* Add types for sm-crypto

* fix Definitions comment in index.d.ts
This commit is contained in:
Thermod
2020-02-25 12:49:07 -08:00
committed by GitHub
parent 6f13c3cbd3
commit 040be0b1cc
4 changed files with 138 additions and 0 deletions
+48
View File
@@ -0,0 +1,48 @@
// Type definitions for sm-crypto 0.1
// Project: https://github.com/JuneAndGreen/sm-crypto#readme
// Definitions by: Thermod <https://github.com/Moonisky>
// 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[];
}
+66
View File
@@ -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);
+23
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }