gzjs-utils
Version:
smart js utils
152 lines (151 loc) • 6.15 kB
TypeScript
export type IV = number[] | string | Uint8Array | Int8Array;
export type Options = {
/**
* 填充方式 pck7padding
*/
padding?: string | "PKCS#5" | "PKCS#7" | "NoPadding" | "PKCS5Padding" | "PKCS7Padding" | "pkcs5padding" | "pkcs7padding";
/**
* 16 bytes iv
*/
iv?: IV;
/**
* 分段大小
*/
segmentSize?: number;
/**
* CTR 模式参数 向量初始值
*/
count?: IV | number;
};
declare const provders: {
ECB: {
/**
* ECB 模式 加密
* @param key 加密密钥
* @param plaintext 待加密的数据内容
* @param opts
*/
encrypt(key: string, plaintext: Uint8Array | Int8Array, opts?: Pick<Options, "padding">): Uint8Array;
decrypt(key: string, ciphertext: Uint8Array | Int8Array, opts?: Pick<Options, "padding">): Uint8Array;
encode(key: string, plaintext: string, opts?: Pick<Options, "padding">): string;
decode(key: string, ciphertext: string, opts?: Pick<Options, "padding">): string;
};
CBC: {
encrypt(key: string, plaintext: Uint8Array | Int8Array, opts?: Pick<Options, "padding" | "iv">): Uint8Array;
decrypt(key: string, ciphertext: Uint8Array | Int8Array, opts?: Pick<Options, "padding" | "iv">): Uint8Array;
encode(key: string, plaintext: string, opts?: Pick<Options, "padding" | "iv">): string;
decode(key: string, ciphertext: string, opts?: Pick<Options, "padding" | "iv">): string;
};
CFB: {
encrypt(key: string, plaintext: Uint8Array | Int8Array, opts?: Pick<Options, "padding" | "iv" | "segmentSize">): Uint8Array;
decrypt(key: string, ciphertext: Uint8Array | Int8Array, opts?: Pick<Options, "padding" | "iv" | "segmentSize">): Uint8Array;
encode(key: string, plaintext: string, opts?: Pick<Options, "padding" | "iv" | "segmentSize">): string;
decode(key: string, ciphertext: string, opts?: Pick<Options, "padding" | "iv" | "segmentSize">): string;
};
OFB: {
encrypt(key: string, plaintext: Uint8Array | Int8Array, opts?: Pick<Options, "padding" | "iv">): Uint8Array;
decrypt(key: string, ciphertext: Uint8Array | Int8Array, opts?: Pick<Options, "padding" | "iv">): Uint8Array;
encode(key: string, plaintext: string, opts?: Pick<Options, "padding" | "iv">): string;
decode(key: string, ciphertext: string, opts?: Pick<Options, "padding" | "iv">): string;
};
CTR: {
encrypt(key: string, plaintext: Uint8Array | Int8Array, opts?: Pick<Options, "padding" | "iv">): Uint8Array;
decrypt(key: string, ciphertext: Uint8Array | Int8Array, opts?: Pick<Options, "padding" | "iv">): Uint8Array;
encode(key: string, plaintext: string, opts?: Pick<Options, "padding" | "iv">): string;
decode(key: string, ciphertext: string, opts?: Pick<Options, "padding" | "iv">): string;
};
};
export type Mode = keyof typeof provders | string;
/**
* AES 加解密函数, 默认使用 ECB模式,
*
* A pure JavaScript implementation of the AES block cipher algorithm and all common modes of operation (CBC, CFB, CTR, ECB and OFB).
* Features --------
*
* <ul>
* <li>Pure JavaScript (with no dependencies)</li>
* <li>Supports all key sizes (128-bit, 192-bit and 256-bit)</li>
* <li>Supports all common modes of operation (CBC, CFB, CTR, ECB and OFB)</li>
* <li>Works in either node.js or web browsers</li>
* </ul>
*
* <h1>API</h1>
* <strong>Node.js</strong>
* <span>To install <code>gzjs-utils</code> in your node.js project:</span>
* <pre>npm install gzjs-utils</pre>
*
* 通用用法 为 <code>浏览器引入js 后 会注册全局变量JsUtils</code>
* <pre>
* const JsUtils = require('gzjs-utils');
*
*
* const iv = "<IV>" // iv must 16 bytes
* const key = "<aesKey>"; // keys must be 128 bits (16 bytes), 192 bits (24 bytes) or 256 bits (32 bytes)
* const mode = '<mode>' // ECB,CBC, CFB,OFB,CTR
* const padding = "PKCS#7" // 填充方式, PKCS#7 | PKCS#5 | NoPadding
* const plaintext = '<plaintext>';
* const ciphertext = '<plaintext>';
*
* JsUtils.AES.encode(key, <plaintext>, {mode: <mode>, iv: <iv>, padding: <padding>}); // return hex string
* JsUtils.AES.encrypt(key, <plaintext>, {mode: <mode>, iv: <iv>, padding: <padding>}); // return Uint8Array
*
* JsUtils.AES.decode(key, <ciphertext>, {mode: <mode>, iv: <iv>, padding: <padding>}); // return plaintext
* JsUtils.AES.decrypt(key, <ciphertext>, {mode: <mode>, iv: <iv>, padding: <padding>}); // return plaintext as Uint8Array
*
* </pre>
*
*/
export type AES = typeof provders & {
/**
* 获取允许的加密模式
*/
modes(): string[];
/**
* 加密函数
* @param text 待加密内容
* @param key 加密密钥
* @param opts 加密套件参数
* @return 返回 Uint8Array 数据数组
*/
encrypt(key: string, text: string | Int8Array | Uint8Array, opts?: Options & {
mode?: Mode;
}): Uint8Array;
/**
* 解密
* @param text 加密的内容 hex内容格式
* @param key 解密密钥
* @param opts 加密套件参数
* @return 返回 Uint8Array 数据数组
*/
decrypt(key: string, text: string | Int8Array | Uint8Array, opts?: Options & {
mode?: Mode;
}): Uint8Array;
/**
* 加密密钥
* @param text 待加密内容
* @param key 加密密钥
* @param opts 加密套件参数
* @return 加密后的 hex 内容
*/
encode(key: string, text: string | Int8Array | Uint8Array, opts?: Options & {
mode?: Mode;
}): string;
/**
* 解密
* @param text 加密的内容 hex内容格式
* @param key 解密密钥
* @param opts 加密套件参数
* @return 解密后的字符串
*/
decode(key: string, text: string | Int8Array | Uint8Array, opts?: Options & {
mode?: Mode;
}): string;
};
export type Opts = Options & {
/**
* 加密模式
*/
mode?: Mode;
};
export declare const _AES_: AES;
export default _AES_;