🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

mima-kit

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mima-kit - npm Package Compare versions

Comparing version
0.0.17
to
0.0.18
+1
-1
dist/chunk/cipher/blockCipher/aes.js

@@ -0,3 +1,3 @@

import { createCipher } from '../../core/cipher';
import { KitError, U8 } from '../../core/utils';
import { createCipher } from '../../core/cipher';
// * Constants

@@ -4,0 +4,0 @@ const SBox = new Uint8Array([0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76, 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15, 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75, 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF, 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8, 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73, 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB, 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08, 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A, 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF, 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16]);

import { createCipher } from '../../core/cipher';
import { KitError, U8, genBitMask, resizeBuffer, rotateL, rotateR } from '../../core/utils';
import { genBitMask, KitError, resizeBuffer, rotateL, rotateR, U8 } from '../../core/utils';
// const Eul = [0xB7, 0xE1, 0x51, 0x62, 0x8A, 0xED, 0x2A, 0x6A, 0xBF, 0x71, 0x58, 0x80, 0x9C, 0xF4, 0xF3, 0xC7, 0x62, 0xE7, 0x16, 0x0F, 0x38, 0xB4, 0xDA, 0x56, 0xA7, 0x84, 0xD9, 0x04, 0x51, 0x90, 0xCF, 0xEF]

@@ -4,0 +4,0 @@ // const Phi = [0x9E, 0x37, 0x79, 0xB9, 0x7F, 0x4A, 0x7C, 0x15, 0xF3, 0x9C, 0xC0, 0x60, 0x5C, 0xED, 0xC8, 0x34, 0x10, 0x82, 0x27, 0x6B, 0xF3, 0xA2, 0x72, 0x51, 0xF8, 0x6C, 0x6A, 0x11, 0xD0, 0xC1, 0x8E, 0x95]

import { createCipher } from '../../core/cipher';
import { KitError, U8, rotateL32, rotateR32 } from '../../core/utils';
import { KitError, rotateL32, rotateR32, U8 } from '../../core/utils';
// * Constants

@@ -4,0 +4,0 @@ const SBox1_1110 = new Uint32Array([0x70707000, 0x82828200, 0x2C2C2C00, 0xECECEC00, 0xB3B3B300, 0x27272700, 0xC0C0C000, 0xE5E5E500, 0xE4E4E400, 0x85858500, 0x57575700, 0x35353500, 0xEAEAEA00, 0x0C0C0C00, 0xAEAEAE00, 0x41414100, 0x23232300, 0xEFEFEF00, 0x6B6B6B00, 0x93939300, 0x45454500, 0x19191900, 0xA5A5A500, 0x21212100, 0xEDEDED00, 0x0E0E0E00, 0x4F4F4F00, 0x4E4E4E00, 0x1D1D1D00, 0x65656500, 0x92929200, 0xBDBDBD00, 0x86868600, 0xB8B8B800, 0xAFAFAF00, 0x8F8F8F00, 0x7C7C7C00, 0xEBEBEB00, 0x1F1F1F00, 0xCECECE00, 0x3E3E3E00, 0x30303000, 0xDCDCDC00, 0x5F5F5F00, 0x5E5E5E00, 0xC5C5C500, 0x0B0B0B00, 0x1A1A1A00, 0xA6A6A600, 0xE1E1E100, 0x39393900, 0xCACACA00, 0xD5D5D500, 0x47474700, 0x5D5D5D00, 0x3D3D3D00, 0xD9D9D900, 0x01010100, 0x5A5A5A00, 0xD6D6D600, 0x51515100, 0x56565600, 0x6C6C6C00, 0x4D4D4D00, 0x8B8B8B00, 0x0D0D0D00, 0x9A9A9A00, 0x66666600, 0xFBFBFB00, 0xCCCCCC00, 0xB0B0B000, 0x2D2D2D00, 0x74747400, 0x12121200, 0x2B2B2B00, 0x20202000, 0xF0F0F000, 0xB1B1B100, 0x84848400, 0x99999900, 0xDFDFDF00, 0x4C4C4C00, 0xCBCBCB00, 0xC2C2C200, 0x34343400, 0x7E7E7E00, 0x76767600, 0x05050500, 0x6D6D6D00, 0xB7B7B700, 0xA9A9A900, 0x31313100, 0xD1D1D100, 0x17171700, 0x04040400, 0xD7D7D700, 0x14141400, 0x58585800, 0x3A3A3A00, 0x61616100, 0xDEDEDE00, 0x1B1B1B00, 0x11111100, 0x1C1C1C00, 0x32323200, 0x0F0F0F00, 0x9C9C9C00, 0x16161600, 0x53535300, 0x18181800, 0xF2F2F200, 0x22222200, 0xFEFEFE00, 0x44444400, 0xCFCFCF00, 0xB2B2B200, 0xC3C3C300, 0xB5B5B500, 0x7A7A7A00, 0x91919100, 0x24242400, 0x08080800, 0xE8E8E800, 0xA8A8A800, 0x60606000, 0xFCFCFC00, 0x69696900, 0x50505000, 0xAAAAAA00, 0xD0D0D000, 0xA0A0A000, 0x7D7D7D00, 0xA1A1A100, 0x89898900, 0x62626200, 0x97979700, 0x54545400, 0x5B5B5B00, 0x1E1E1E00, 0x95959500, 0xE0E0E000, 0xFFFFFF00, 0x64646400, 0xD2D2D200, 0x10101000, 0xC4C4C400, 0x00000000, 0x48484800, 0xA3A3A300, 0xF7F7F700, 0x75757500, 0xDBDBDB00, 0x8A8A8A00, 0x03030300, 0xE6E6E600, 0xDADADA00, 0x09090900, 0x3F3F3F00, 0xDDDDDD00, 0x94949400, 0x87878700, 0x5C5C5C00, 0x83838300, 0x02020200, 0xCDCDCD00, 0x4A4A4A00, 0x90909000, 0x33333300, 0x73737300, 0x67676700, 0xF6F6F600, 0xF3F3F300, 0x9D9D9D00, 0x7F7F7F00, 0xBFBFBF00, 0xE2E2E200, 0x52525200, 0x9B9B9B00, 0xD8D8D800, 0x26262600, 0xC8C8C800, 0x37373700, 0xC6C6C600, 0x3B3B3B00, 0x81818100, 0x96969600, 0x6F6F6F00, 0x4B4B4B00, 0x13131300, 0xBEBEBE00, 0x63636300, 0x2E2E2E00, 0xE9E9E900, 0x79797900, 0xA7A7A700, 0x8C8C8C00, 0x9F9F9F00, 0x6E6E6E00, 0xBCBCBC00, 0x8E8E8E00, 0x29292900, 0xF5F5F500, 0xF9F9F900, 0xB6B6B600, 0x2F2F2F00, 0xFDFDFD00, 0xB4B4B400, 0x59595900, 0x78787800, 0x98989800, 0x06060600, 0x6A6A6A00, 0xE7E7E700, 0x46464600, 0x71717100, 0xBABABA00, 0xD4D4D400, 0x25252500, 0xABABAB00, 0x42424200, 0x88888800, 0xA2A2A200, 0x8D8D8D00, 0xFAFAFA00, 0x72727200, 0x07070700, 0xB9B9B900, 0x55555500, 0xF8F8F800, 0xEEEEEE00, 0xACACAC00, 0x0A0A0A00, 0x36363600, 0x49494900, 0x2A2A2A00, 0x68686800, 0x3C3C3C00, 0x38383800, 0xF1F1F100, 0xA4A4A400, 0x40404000, 0x28282800, 0xD3D3D300, 0x7B7B7B00, 0xBBBBBB00, 0xC9C9C900, 0x43434300, 0xC1C1C100, 0x15151500, 0xE3E3E300, 0xADADAD00, 0xF4F4F400, 0x77777700, 0xC7C7C700, 0x80808000, 0x9E9E9E00]);

@@ -0,3 +1,3 @@

import { createCipher } from '../../core/cipher';
import { KitError, U8 } from '../../core/utils';
import { createCipher } from '../../core/cipher';
// * Constants

@@ -4,0 +4,0 @@ const IP = [58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7].map(value => 64 - value);

@@ -1,3 +0,3 @@

import { KitError, U8, rotateL32 } from '../../core/utils';
import { createCipher } from '../../core/cipher';
import { KitError, rotateL32, U8 } from '../../core/utils';
// * Constants

@@ -4,0 +4,0 @@ const SBox = new Uint8Array([0xD6, 0x90, 0xE9, 0xFE, 0xCC, 0xE1, 0x3D, 0xB7, 0x16, 0xB6, 0x14, 0xC2, 0x28, 0xFB, 0x2C, 0x05, 0x2B, 0x67, 0x9A, 0x76, 0x2A, 0xBE, 0x04, 0xC3, 0xAA, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, 0x9C, 0x42, 0x50, 0xF4, 0x91, 0xEF, 0x98, 0x7A, 0x33, 0x54, 0x0B, 0x43, 0xED, 0xCF, 0xAC, 0x62, 0xE4, 0xB3, 0x1C, 0xA9, 0xC9, 0x08, 0xE8, 0x95, 0x80, 0xDF, 0x94, 0xFA, 0x75, 0x8F, 0x3F, 0xA6, 0x47, 0x07, 0xA7, 0xFC, 0xF3, 0x73, 0x17, 0xBA, 0x83, 0x59, 0x3C, 0x19, 0xE6, 0x85, 0x4F, 0xA8, 0x68, 0x6B, 0x81, 0xB2, 0x71, 0x64, 0xDA, 0x8B, 0xF8, 0xEB, 0x0F, 0x4B, 0x70, 0x56, 0x9D, 0x35, 0x1E, 0x24, 0x0E, 0x5E, 0x63, 0x58, 0xD1, 0xA2, 0x25, 0x22, 0x7C, 0x3B, 0x01, 0x21, 0x78, 0x87, 0xD4, 0x00, 0x46, 0x57, 0x9F, 0xD3, 0x27, 0x52, 0x4C, 0x36, 0x02, 0xE7, 0xA0, 0xC4, 0xC8, 0x9E, 0xEA, 0xBF, 0x8A, 0xD2, 0x40, 0xC7, 0x38, 0xB5, 0xA3, 0xF7, 0xF2, 0xCE, 0xF9, 0x61, 0x15, 0xA1, 0xE0, 0xAE, 0x5D, 0xA4, 0x9B, 0x34, 0x1A, 0x55, 0xAD, 0x93, 0x32, 0x30, 0xF5, 0x8C, 0xB1, 0xE3, 0x1D, 0xF6, 0xE2, 0x2E, 0x82, 0x66, 0xCA, 0x60, 0xC0, 0x29, 0x23, 0xAB, 0x0D, 0x53, 0x4E, 0x6F, 0xD5, 0xDB, 0x37, 0x45, 0xDE, 0xFD, 0x8E, 0x2F, 0x03, 0xFF, 0x6A, 0x72, 0x6D, 0x6C, 0x5B, 0x51, 0x8D, 0x1B, 0xAF, 0x92, 0xBB, 0xDD, 0xBC, 0x7F, 0x11, 0xD9, 0x5C, 0x41, 0x1F, 0x10, 0x5A, 0xD8, 0x0A, 0xC1, 0x31, 0x88, 0xA5, 0xCD, 0x7B, 0xBD, 0x2D, 0x74, 0xD0, 0x12, 0xB8, 0xE5, 0xB4, 0xB0, 0x89, 0x69, 0x97, 0x4A, 0x0C, 0x96, 0x77, 0x7E, 0x65, 0xB9, 0xF1, 0x09, 0xC5, 0x6E, 0xC6, 0x84, 0x18, 0xF0, 0x7D, 0xEC, 0x3A, 0xDC, 0x4D, 0x20, 0x79, 0xEE, 0x5F, 0x3E, 0xD7, 0xCB, 0x39, 0x48]);

@@ -1,2 +0,2 @@

import { PKCS7_PAD, createCipher } from '../../core/cipher';
import { createCipher, PKCS7_PAD } from '../../core/cipher';
import { KitError, U8 } from '../../core/utils';

@@ -175,6 +175,2 @@ // * Constants

* Corrected Block TEA (XXTEA) block cipher algorithm
*
* @param {Padding} [config.padding] - 填充方式 / Padding method (default: PKCS7)
* @param {number} [config.round] - 轮数 / Rounds (default: undefined)
* @param {number} [config.BLOCK_SIZE] - 分组大小 / Block size (default: 16)
*/

@@ -181,0 +177,0 @@ export function xxtea(config) {

import { createCipher } from '../../core/cipher';
import { KitError, U8, rotateL32, rotateR32 } from '../../core/utils';
import { KitError, rotateL32, rotateR32, U8 } from '../../core/utils';
// * Constants

@@ -4,0 +4,0 @@ const P0 = new Uint8Array([0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 0x9A, 0x92, 0x80, 0x78, 0xE4, 0xDD, 0xD1, 0x38, 0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C, 0x43, 0x75, 0x37, 0x26, 0xFA, 0x13, 0x94, 0x48, 0xF2, 0xD0, 0x8B, 0x30, 0x84, 0x54, 0xDF, 0x23, 0x19, 0x5B, 0x3D, 0x59, 0xF3, 0xAE, 0xA2, 0x82, 0x63, 0x01, 0x83, 0x2E, 0xD9, 0x51, 0x9B, 0x7C, 0xA6, 0xEB, 0xA5, 0xBE, 0x16, 0x0C, 0xE3, 0x61, 0xC0, 0x8C, 0x3A, 0xF5, 0x73, 0x2C, 0x25, 0x0B, 0xBB, 0x4E, 0x89, 0x6B, 0x53, 0x6A, 0xB4, 0xF1, 0xE1, 0xE6, 0xBD, 0x45, 0xE2, 0xF4, 0xB6, 0x66, 0xCC, 0x95, 0x03, 0x56, 0xD4, 0x1C, 0x1E, 0xD7, 0xFB, 0xC3, 0x8E, 0xB5, 0xE9, 0xCF, 0xBF, 0xBA, 0xEA, 0x77, 0x39, 0xAF, 0x33, 0xC9, 0x62, 0x71, 0x81, 0x79, 0x09, 0xAD, 0x24, 0xCD, 0xF9, 0xD8, 0xE5, 0xC5, 0xB9, 0x4D, 0x44, 0x08, 0x86, 0xE7, 0xA1, 0x1D, 0xAA, 0xED, 0x06, 0x70, 0xB2, 0xD2, 0x41, 0x7B, 0xA0, 0x11, 0x31, 0xC2, 0x27, 0x90, 0x20, 0xF6, 0x60, 0xFF, 0x96, 0x5C, 0xB1, 0xAB, 0x9E, 0x9C, 0x52, 0x1B, 0x5F, 0x93, 0x0A, 0xEF, 0x91, 0x85, 0x49, 0xEE, 0x2D, 0x4F, 0x8F, 0x3B, 0x47, 0x87, 0x6D, 0x46, 0xD6, 0x3E, 0x69, 0x64, 0x2A, 0xCE, 0xCB, 0x2F, 0xFC, 0x97, 0x05, 0x7A, 0xAC, 0x7F, 0xD5, 0x1A, 0x4B, 0x0E, 0xA7, 0x5A, 0x28, 0x14, 0x3F, 0x29, 0x88, 0x3C, 0x4C, 0x02, 0xB8, 0xDA, 0xB0, 0x17, 0x55, 0x1F, 0x8A, 0x7D, 0x57, 0xC7, 0x8D, 0x74, 0xB7, 0xC4, 0x9F, 0x72, 0x7E, 0x15, 0x22, 0x12, 0x58, 0x07, 0x99, 0x34, 0x6E, 0x50, 0xDE, 0x68, 0x65, 0xBC, 0xDB, 0xF8, 0xC8, 0xA8, 0x2B, 0x40, 0xDC, 0xFE, 0x32, 0xA4, 0xCA, 0x10, 0x21, 0xF0, 0xD3, 0x5D, 0x0F, 0x00, 0x6F, 0x9D, 0x36, 0x42, 0x4A, 0x5E, 0xC1, 0xE0]);

import { cbc, createCipher } from '../../core/cipher';
import { BIPoint, Fp, FpEC, U8Point } from '../../core/ec';
import { KitError, U8, genBitMask, genRandomBI, getBIBits, joinBuffer, mod, modInverse } from '../../core/utils';
import { x963kdf } from '../../core/kdf';
import { genBitMask, genRandomBI, getBIBits, joinBuffer, KitError, mod, modInverse, U8 } from '../../core/utils';
import { hmac } from '../../hash/hmac';
import { sha256 } from '../../hash/sha256';
import { aes } from '../blockCipher/aes';
import { sha256 } from '../../hash/sha256';
import { hmac } from '../../hash/hmac';
// * Functions

@@ -13,9 +13,2 @@ /**

* Define ECIES Configuration
*
* @param {IVBlockCipher} [config.cipher] - 分组密码算法 / Block Cipher Algorithm (default: AES-256-GCM)
* @param {KeyHash} [config.mac] - 密钥哈希函数 / Key Hash Function (default: HMAC-SHA-256)
* @param {KDF} [config.kdf] - 密钥派生函数 / Key Derivation Function (default: ANSI-X9.63-KDF with SHA-256)
* @param {Uint8Array} [config.S1] - 附加数据1 / Additional Data 1 (default: empty)
* @param {Uint8Array} [config.S2] - 附加数据2 / Additional Data 2 (default: empty)
* @param {Uint8Array} [config.iv] - 初始化向量 / Initialization Vector (default: Uint8Array(cipher.BLOCK_SIZE))
*/

@@ -277,3 +270,3 @@ export function defineECIES(config) {

const Z = deriveShare.x;
const K = kdf((cipher.KEY_SIZE + mac.KEY_SIZE) << 3, joinBuffer(Z, S1));
const K = kdf((cipher.KEY_SIZE + mac.KEY_SIZE), joinBuffer(Z, S1));
const KE = K.slice(0, cipher.KEY_SIZE);

@@ -295,3 +288,3 @@ const KM = K.slice(cipher.KEY_SIZE, cipher.KEY_SIZE + mac.KEY_SIZE);

const Z = deriveShare.x;
const K = kdf((cipher.KEY_SIZE + mac.KEY_SIZE) << 3, joinBuffer(Z, S1));
const K = kdf((cipher.KEY_SIZE + mac.KEY_SIZE), joinBuffer(Z, S1));
const KE = K.slice(0, cipher.KEY_SIZE);

@@ -298,0 +291,0 @@ const KM = K.slice(cipher.KEY_SIZE, cipher.KEY_SIZE + mac.KEY_SIZE);

import { ASN1 } from '../../core/asn1';
import { Counter, KitError, U8, getBIBits, joinBuffer } from '../../core/utils';
import { Counter, getBIBits, joinBuffer, KitError, U8 } from '../../core/utils';
import { sha256 } from '../../hash/sha256';

@@ -4,0 +4,0 @@ import { rsa } from './rsa';

import { genPrime } from '../../core/prime';
import { KitError, U8, gcd, lcm, mod, modInverse, modPow } from '../../core/utils';
import { gcd, KitError, lcm, mod, modInverse, modPow, U8 } from '../../core/utils';
// * RSA Algorithm

@@ -4,0 +4,0 @@ /**

import { sm2p256v1 } from '../../core/ecParams';
import { x963kdf } from '../../core/kdf';
import { genBitMask, getBIBits, joinBuffer, KitError, mod, modInverse, U8 } from '../../core/utils';
import { sm3 } from '../../hash/sm3';
import { KitError, U8, genBitMask, getBIBits, joinBuffer, mod, modInverse } from '../../core/utils';
import { FpECC } from './ecc';

@@ -122,3 +122,3 @@ /**

const ikm = joinBuffer(x2, y2);
const C2 = kdf(M.length << 3, ikm);
const C2 = kdf(M.length, ikm);
C2.forEach((_, i) => C2[i] ^= M[i]);

@@ -146,3 +146,3 @@ const C3 = hash(joinBuffer(x2, M, y2));

const ikm = joinBuffer(x2, y2);
const t = kdf(C2_Length << 3, ikm);
const t = kdf(C2_Length, ikm);
let C2;

@@ -149,0 +149,0 @@ let C3;

import { Fp } from '../../core/ec';
import { curve25519, curve448 } from '../../core/ecParams';
import { KitError, U8, genRandomBI, getBIBits } from '../../core/utils';
import { curve448, curve25519 } from '../../core/ecParams';
import { genRandomBI, getBIBits, KitError, U8 } from '../../core/utils';
// * X25519 & X448 Algorithms

@@ -5,0 +5,0 @@ function cSwap(swap, x_2, x_3) {

import { createCipher } from '../../core/cipher';
import { KitError, U8, resizeBuffer, rotateL32 } from '../../core/utils';
import { KitError, resizeBuffer, rotateL32, U8 } from '../../core/utils';
// * Constants

@@ -4,0 +4,0 @@ const A = new Uint32Array([0x4D34D34D, 0xD34D34D3, 0x34D34D34, 0x4D34D34D, 0xD34D34D3, 0x34D34D34, 0x4D34D34D, 0xD34D34D3]);

import { createCipher } from '../../core/cipher';
import { Counter, KitError, U8, resizeBuffer, rotateL32 } from '../../core/utils';
import { Counter, KitError, resizeBuffer, rotateL32, U8, u32 } from '../../core/utils';
// * Functions
// eslint-disable-next-line unused-imports/no-unused-vars
function QR(a, b, c, d) {

@@ -13,3 +14,3 @@ b ^= rotateL32(a + d, 7);

// to word
const X = new Uint32Array(x.buffer);
const X = u32(x);
const W = X.slice(0);

@@ -19,19 +20,49 @@ // main loop

// ODD Rounds
[W[0], W[4], W[8], W[12]] = QR(W[0], W[4], W[8], W[12]);
[W[5], W[9], W[13], W[1]] = QR(W[5], W[9], W[13], W[1]);
[W[10], W[14], W[2], W[6]] = QR(W[10], W[14], W[2], W[6]);
[W[15], W[3], W[7], W[11]] = QR(W[15], W[3], W[7], W[11]);
// [W[0], W[4], W[8], W[12]] = QR(W[0], W[4], W[8], W[12]);
// [W[5], W[9], W[13], W[1]] = QR(W[5], W[9], W[13], W[1]);
// [W[10], W[14], W[2], W[6]] = QR(W[10], W[14], W[2], W[6]);
// [W[15], W[3], W[7], W[11]] = QR(W[15], W[3], W[7], W[11]);
// EVEN Rounds
[W[0], W[1], W[2], W[3]] = QR(W[0], W[1], W[2], W[3]);
[W[5], W[6], W[7], W[4]] = QR(W[5], W[6], W[7], W[4]);
[W[10], W[11], W[8], W[9]] = QR(W[10], W[11], W[8], W[9]);
[W[15], W[12], W[13], W[14]] = QR(W[15], W[12], W[13], W[14]);
// [W[0], W[1], W[2], W[3]] = QR(W[0], W[1], W[2], W[3]);
// [W[5], W[6], W[7], W[4]] = QR(W[5], W[6], W[7], W[4]);
// [W[10], W[11], W[8], W[9]] = QR(W[10], W[11], W[8], W[9]);
// [W[15], W[12], W[13], W[14]] = QR(W[15], W[12], W[13], W[14])
W[4] ^= rotateL32(W[0] + W[12], 7);
W[8] ^= rotateL32(W[4] + W[0], 9);
W[12] ^= rotateL32(W[8] + W[4], 13);
W[0] ^= rotateL32(W[12] + W[8], 18);
W[9] ^= rotateL32(W[5] + W[1], 7);
W[13] ^= rotateL32(W[9] + W[5], 9);
W[1] ^= rotateL32(W[13] + W[9], 13);
W[5] ^= rotateL32(W[1] + W[13], 18);
W[14] ^= rotateL32(W[10] + W[6], 7);
W[2] ^= rotateL32(W[14] + W[10], 9);
W[6] ^= rotateL32(W[2] + W[14], 13);
W[10] ^= rotateL32(W[6] + W[2], 18);
W[3] ^= rotateL32(W[15] + W[11], 7);
W[7] ^= rotateL32(W[3] + W[15], 9);
W[11] ^= rotateL32(W[7] + W[3], 13);
W[15] ^= rotateL32(W[11] + W[7], 18);
W[1] ^= rotateL32(W[0] + W[3], 7);
W[2] ^= rotateL32(W[1] + W[0], 9);
W[3] ^= rotateL32(W[2] + W[1], 13);
W[0] ^= rotateL32(W[3] + W[2], 18);
W[6] ^= rotateL32(W[5] + W[4], 7);
W[7] ^= rotateL32(W[6] + W[5], 9);
W[4] ^= rotateL32(W[7] + W[6], 13);
W[5] ^= rotateL32(W[4] + W[7], 18);
W[11] ^= rotateL32(W[10] + W[9], 7);
W[8] ^= rotateL32(W[11] + W[10], 9);
W[9] ^= rotateL32(W[8] + W[11], 13);
W[10] ^= rotateL32(W[9] + W[8], 18);
W[12] ^= rotateL32(W[15] + W[14], 7);
W[13] ^= rotateL32(W[12] + W[15], 9);
W[14] ^= rotateL32(W[13] + W[12], 13);
W[15] ^= rotateL32(W[14] + W[13], 18);
}
// mix
const Z = new U8(64);
const Z32 = new Uint32Array(Z.buffer);
for (let i = 0; i < 16; i++) {
Z32[i] = X[i] + W[i];
W[i] += X[i];
}
return Z;
return new U8(W.buffer);
}

@@ -43,5 +74,5 @@ function expand(K, iv) {

const S = new Counter(64);
const S32 = new Uint32Array(S.buffer);
const K32 = new Uint32Array(K.buffer);
const N32 = new Uint32Array(iv.buffer);
const S32 = u32(S);
const K32 = u32(K);
const N32 = u32(iv);
switch (K.byteLength) {

@@ -48,0 +79,0 @@ case 16: // use tau

@@ -1,2 +0,2 @@

import { KitError, U8, rotateL32, wrap } from '../../core/utils';
import { KitError, rotateL32, U8, wrap } from '../../core/utils';
// * Constants

@@ -3,0 +3,0 @@ const S0 = new Uint8Array([0x3E, 0x72, 0x5B, 0x47, 0xCA, 0xE0, 0x00, 0x33, 0x04, 0xD1, 0x54, 0x98, 0x09, 0xB9, 0x6D, 0xCB, 0x7B, 0x1B, 0xF9, 0x32, 0xAF, 0x9D, 0x6A, 0xA5, 0xB8, 0x2D, 0xFC, 0x1D, 0x08, 0x53, 0x03, 0x90, 0x4D, 0x4E, 0x84, 0x99, 0xE4, 0xCE, 0xD9, 0x91, 0xDD, 0xB6, 0x85, 0x48, 0x8B, 0x29, 0x6E, 0xAC, 0xCD, 0xC1, 0xF8, 0x1E, 0x73, 0x43, 0x69, 0xC6, 0xB5, 0xBD, 0xFD, 0x39, 0x63, 0x20, 0xD4, 0x38, 0x76, 0x7D, 0xB2, 0xA7, 0xCF, 0xED, 0x57, 0xC5, 0xF3, 0x2C, 0xBB, 0x14, 0x21, 0x06, 0x55, 0x9B, 0xE3, 0xEF, 0x5E, 0x31, 0x4F, 0x7F, 0x5A, 0xA4, 0x0D, 0x82, 0x51, 0x49, 0x5F, 0xBA, 0x58, 0x1C, 0x4A, 0x16, 0xD5, 0x17, 0xA8, 0x92, 0x24, 0x1F, 0x8C, 0xFF, 0xD8, 0xAE, 0x2E, 0x01, 0xD3, 0xAD, 0x3B, 0x4B, 0xDA, 0x46, 0xEB, 0xC9, 0xDE, 0x9A, 0x8F, 0x87, 0xD7, 0x3A, 0x80, 0x6F, 0x2F, 0xC8, 0xB1, 0xB4, 0x37, 0xF7, 0x0A, 0x22, 0x13, 0x28, 0x7C, 0xCC, 0x3C, 0x89, 0xC7, 0xC3, 0x96, 0x56, 0x07, 0xBF, 0x7E, 0xF0, 0x0B, 0x2B, 0x97, 0x52, 0x35, 0x41, 0x79, 0x61, 0xA6, 0x4C, 0x10, 0xFE, 0xBC, 0x26, 0x95, 0x88, 0x8A, 0xB0, 0xA3, 0xFB, 0xC0, 0x18, 0x94, 0xF2, 0xE1, 0xE5, 0xE9, 0x5D, 0xD0, 0xDC, 0x11, 0x66, 0x64, 0x5C, 0xEC, 0x59, 0x42, 0x75, 0x12, 0xF5, 0x74, 0x9C, 0xAA, 0x23, 0x0E, 0x86, 0xAB, 0xBE, 0x2A, 0x02, 0xE7, 0x67, 0xE6, 0x44, 0xA2, 0x6C, 0xC2, 0x93, 0x9F, 0xF1, 0xF6, 0xFA, 0x36, 0xD2, 0x50, 0x68, 0x9E, 0x62, 0x71, 0x15, 0x3D, 0xD6, 0x40, 0xC4, 0xE2, 0x0F, 0x8E, 0x83, 0x77, 0x6B, 0x25, 0x05, 0x3F, 0x0C, 0x30, 0xEA, 0x70, 0xB7, 0xA1, 0xE8, 0xA9, 0x65, 0x8D, 0x27, 0x1A, 0xDB, 0x81, 0xB3, 0xA0, 0xF4, 0x45, 0x7A, 0x19, 0xDF, 0xEE, 0x78, 0x34, 0x60]);

@@ -1,2 +0,2 @@

import { Counter, KitError, U8, joinBuffer, wrap } from './utils';
import { Counter, joinBuffer, KitError, U8, wrap } from './utils';
export function createCipher(algorithm, description) {

@@ -3,0 +3,0 @@ return wrap((key, iv) => wrap(algorithm(key, iv), description), description);

@@ -1,10 +0,7 @@

import { KitError, U8, wrap } from './utils';
import { KitError, trying, U8, wrap } from './utils';
function createCodec(parse, stringify, format) {
function codec(input) {
if (typeof input === 'string') {
return parse(input);
}
else {
return stringify(input);
}
return typeof input === 'string'
? parse(input)
: stringify(input);
}

@@ -18,36 +15,34 @@ return wrap(codec, { FORMAT: format });

*/
try {
const buffer = new TextEncoder().encode(input);
return U8.from(buffer);
const [error, result] = trying(() => new TextEncoder().encode(input));
if (!error) {
return U8.from(result);
}
/** provided by xingluo233 */
catch {
const buffer = [];
for (let i = 0; i < input.length; i++) {
const char_code = input.codePointAt(i);
if (char_code === undefined) {
continue;
}
else if (char_code < 0x80) {
buffer.push(char_code);
}
else if (char_code < 0x800) {
buffer.push(0xC0 | (char_code >> 6));
buffer.push(0x80 | (char_code & 0x3F));
}
else if (char_code < 0x10000) {
buffer.push(0xE0 | (char_code >> 12));
buffer.push(0x80 | ((char_code >> 6) & 0x3F));
buffer.push(0x80 | (char_code & 0x3F));
}
else if (char_code < 0x110000) {
buffer.push(0xF0 | (char_code >> 18));
buffer.push(0x80 | ((char_code >> 12) & 0x3F));
buffer.push(0x80 | ((char_code >> 6) & 0x3F));
buffer.push(0x80 | (char_code & 0x3F));
i++;
}
const buffer = [];
for (let i = 0; i < input.length; i++) {
const char_code = input.codePointAt(i);
if (char_code === undefined) {
continue;
}
return U8.from(buffer);
else if (char_code < 0x80) {
buffer.push(char_code);
}
else if (char_code < 0x800) {
buffer.push(0xC0 | (char_code >> 6));
buffer.push(0x80 | (char_code & 0x3F));
}
else if (char_code < 0x10000) {
buffer.push(0xE0 | (char_code >> 12));
buffer.push(0x80 | ((char_code >> 6) & 0x3F));
buffer.push(0x80 | (char_code & 0x3F));
}
else if (char_code < 0x110000) {
buffer.push(0xF0 | (char_code >> 18));
buffer.push(0x80 | ((char_code >> 12) & 0x3F));
buffer.push(0x80 | ((char_code >> 6) & 0x3F));
buffer.push(0x80 | (char_code & 0x3F));
i++;
}
}
return U8.from(buffer);
}

@@ -59,38 +54,37 @@ function U8ToUTF8(input) {

*/
try {
return new TextDecoder().decode(input);
const [error, result] = trying(() => new TextDecoder().decode(input));
if (!error) {
return result;
}
/** provided by xingluo233 */
catch {
const str = [];
let i = 0;
while (i < input.length) {
const byte1 = input[i++];
if (byte1 < 0x80) {
str.push(String.fromCharCode(byte1));
}
else if (byte1 >= 0xC0 && byte1 < 0xE0) {
const byte2 = input[i++];
const char_code = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F);
str.push(String.fromCharCode(char_code));
}
else if (byte1 >= 0xE0 && byte1 < 0xF0) {
const byte2 = input[i++];
const byte3 = input[i++];
const char_code = ((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F);
str.push(String.fromCharCode(char_code));
}
else if (byte1 >= 0xF0 && byte1 < 0xF8) {
const byte2 = input[i++];
const byte3 = input[i++];
const byte4 = input[i++];
const char_code = ((byte1 & 0x07) << 18) | ((byte2 & 0x3F) << 12) | ((byte3 & 0x3F) << 6) | (byte4 & 0x3F);
str.push(String.fromCodePoint(char_code));
}
else {
console.warn('Included an invalid UTF-8 byte');
}
const str = [];
let i = 0;
while (i < input.length) {
const byte1 = input[i++];
if (byte1 < 0x80) {
str.push(String.fromCharCode(byte1));
}
return str.join('');
else if (byte1 >= 0xC0 && byte1 < 0xE0) {
const byte2 = input[i++];
const char_code = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F);
str.push(String.fromCharCode(char_code));
}
else if (byte1 >= 0xE0 && byte1 < 0xF0) {
const byte2 = input[i++];
const byte3 = input[i++];
const char_code = ((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F);
str.push(String.fromCharCode(char_code));
}
else if (byte1 >= 0xF0 && byte1 < 0xF8) {
const byte2 = input[i++];
const byte3 = input[i++];
const byte4 = input[i++];
const char_code = ((byte1 & 0x07) << 18) | ((byte2 & 0x3F) << 12) | ((byte3 & 0x3F) << 6) | (byte4 & 0x3F);
str.push(String.fromCodePoint(char_code));
}
else {
console.warn('Included an invalid UTF-8 byte');
}
}
return str.join('');
}

@@ -100,2 +94,7 @@ /** UTF-8 编解码器 / Codec */

function HEXToU8(input) {
// eslint-disable-next-line node/prefer-global/buffer
const [error, result] = trying(() => Buffer.from(input.replace(/[^0-9a-f]/gi, ''), 'hex'));
if (!error) {
return U8.from(result);
}
const arr = input.match(/[\da-f]{2}/gi);

@@ -108,2 +107,7 @@ if (arr == null) {

function U8ToHEX(input) {
// eslint-disable-next-line node/prefer-global/buffer
const [error, str] = trying(() => Buffer.from(input).toString('hex'));
if (!error) {
return str;
}
let result = '';

@@ -118,5 +122,15 @@ for (let i = 0; i < input.length; i++) {

function B64ToU8(input) {
// eslint-disable-next-line node/prefer-global/buffer
const [error, result] = trying(() => Buffer.from(input.replace(/[^A-Z0-9+/]/gi, ''), 'base64'));
if (!error) {
return U8.from(result);
}
return B64CommonParse(input);
}
function U8ToB64(input) {
// eslint-disable-next-line node/prefer-global/buffer
const [error, str] = trying(() => Buffer.from(input).toString('base64'));
if (!error) {
return str;
}
return B64CommonStringify(input, false);

@@ -127,5 +141,15 @@ }

function B64URLToU8(input) {
// eslint-disable-next-line node/prefer-global/buffer
const [error, result] = trying(() => Buffer.from(input.replace(/[^\w\-]/g, ''), 'base64url'));
if (!error) {
return U8.from(result);
}
return B64CommonParse(input);
}
function U8ToB64URL(input) {
// eslint-disable-next-line node/prefer-global/buffer
const [error, str] = trying(() => Buffer.from(input).toString('base64url'));
if (!error) {
return str;
}
return B64(input).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');

@@ -143,3 +167,2 @@ }

* @param {string} input - B64 或 B64url 字符串
* @param {boolean} url - 是否是 B64url 字符串
*/

@@ -146,0 +169,0 @@ function B64CommonParse(input) {

@@ -1,2 +0,2 @@

import { U8, mod, modInverse, modPow, modPrimeSquare } from './utils';
import { mod, modInverse, modPow, modPrimeSquare, U8 } from './utils';
// * FpEC Components

@@ -3,0 +3,0 @@ /**

@@ -1,2 +0,4 @@

import { Counter, joinBuffer } from './utils';
import { hmac } from '../hash/hmac';
import { sha256 } from '../hash/sha256';
import { Counter, joinBuffer, KitError, rotateL32, U8, u32 } from './utils';
/**

@@ -7,9 +9,9 @@ * ANSI-X9.63 Key Derivation Function

*/
export function x963kdf(hash) {
const d_bit = hash.DIGEST_SIZE << 3;
return (k_bit, ikm, info = new Uint8Array(0)) => {
export function x963kdf(hash, info = new Uint8Array(0)) {
const d_byte = hash.DIGEST_SIZE;
return (k_byte, ikm) => {
/** Output Keying Material */
const okm = [];
const counter = new Counter([0, 0, 0, 1]);
for (let okm_bit = 0; okm_bit < k_bit; okm_bit += d_bit) {
for (let okm_byte = 0; okm_byte < k_byte; okm_byte += d_byte) {
const data = joinBuffer(ikm, counter, info);

@@ -19,3 +21,3 @@ okm.push(hash(data));

}
return joinBuffer(...okm).slice(0, k_bit >> 3);
return joinBuffer(...okm).slice(0, k_byte);
};

@@ -28,5 +30,5 @@ }

*/
export function hkdf(k_hash, salt = new Uint8Array(k_hash.DIGEST_SIZE)) {
const d_bit = k_hash.DIGEST_SIZE << 3;
return (k_bit, ikm, info = new Uint8Array(0)) => {
export function hkdf(k_hash, info = new Uint8Array(0)) {
const d_byte = k_hash.DIGEST_SIZE;
return (k_byte, ikm, salt = new Uint8Array(0)) => {
/** Pseudo-Random Key */

@@ -38,3 +40,3 @@ const prk = k_hash(salt, ikm);

let prv = new Uint8Array(0);
for (let okm_bit = 0; okm_bit < k_bit; okm_bit += d_bit) {
for (let okm_byte = 0; okm_byte < k_byte; okm_byte += d_byte) {
prv = k_hash(prk, joinBuffer(prv, info, counter));

@@ -44,3 +46,3 @@ okm.push(prv);

}
return joinBuffer(...okm).slice(0, k_bit >> 3);
return joinBuffer(...okm).slice(0, k_byte);
};

@@ -50,11 +52,9 @@ }

* Password-Based Key Derivation Function 2 (PBKDF2), please combine `hmac` and `hash` externally to control the behavior of calling `hmac` inside the function.
* Also, PBKDF2 does not use the `info` parameter, if provided, it will be ignored.
*
* PBKDF2 密码基础密钥派生函数 (PBKDF2), 请在外部组合 `hmac` 和 `hash` 函数, 以控制在函数内部调用 `hmac` 时的行为.
* 同时, PBKDF2 不使用 `info` 参数, 如果提供 `info`, 将被忽略.
*/
export function pbkdf2(k_hash, salt = new Uint8Array(k_hash.DIGEST_SIZE), iterations = 1000) {
const d_bit = k_hash.DIGEST_SIZE << 3;
return (k_bit, ikm) => {
ikm = joinBuffer(ikm);
export function pbkdf2(k_hash, iterations = 1000) {
const d_byte = k_hash.DIGEST_SIZE;
return (k_byte, ikm, salt = new Uint8Array(0)) => {
ikm = U8.from(ikm);
/** Output Keying Material */

@@ -65,3 +65,3 @@ const okm = [];

const counter = new Counter([0, 0, 0, 1]);
for (let okm_bit = 0; okm_bit < k_bit; okm_bit += d_bit) {
for (let okm_byte = 0; okm_byte < k_byte; okm_byte += d_byte) {
T = new Uint8Array(k_hash.DIGEST_SIZE);

@@ -76,4 +76,155 @@ U = joinBuffer(salt, counter);

}
return joinBuffer(...okm).slice(0, k_bit >> 3);
return joinBuffer(...okm).slice(0, k_byte);
};
}
/**
* Scrypt Key Derivation Function
*
* Scrypt 密钥派生函数
*
* Based on https://github.com/paulmillr/noble-hashes
*/
export function scrypt(config) {
const { N = 16384, r = 8, p = 1, maxmem = 0x40000400, kdf = pbkdf2(hmac(sha256), 1), } = config ?? {};
const BLOCK_SIZE = r << 7;
const BLOCK_SIZE_32 = r << 5;
const MAX_p = (0x1FFFFFFFE0 / BLOCK_SIZE) >>> 0;
const MEM_COST = BLOCK_SIZE * (N + p);
const N_1 = N - 1;
if (N === 0 || (N & N_1) !== 0)
throw new KitError(`N must be a power of 2`);
if (p < 1 || p > MAX_p)
throw new KitError(`p must be in range [1, ${MAX_p}]`);
if (MEM_COST > maxmem)
throw new KitError(`Memory cost exceeds maxmem: ${MEM_COST} > ${maxmem}`);
// 内联 rotateL32 (神秘的黑魔法)
const rotl = rotateL32;
function fast_xor_salsa_hash(prev, pi, input, ii, output, oi) {
const y00 = prev[pi++] ^ input[ii++];
const y01 = prev[pi++] ^ input[ii++];
const y02 = prev[pi++] ^ input[ii++];
const y03 = prev[pi++] ^ input[ii++];
const y04 = prev[pi++] ^ input[ii++];
const y05 = prev[pi++] ^ input[ii++];
const y06 = prev[pi++] ^ input[ii++];
const y07 = prev[pi++] ^ input[ii++];
const y08 = prev[pi++] ^ input[ii++];
const y09 = prev[pi++] ^ input[ii++];
const y10 = prev[pi++] ^ input[ii++];
const y11 = prev[pi++] ^ input[ii++];
const y12 = prev[pi++] ^ input[ii++];
const y13 = prev[pi++] ^ input[ii++];
const y14 = prev[pi++] ^ input[ii++];
const y15 = prev[pi++] ^ input[ii++];
// Save state to temporary variables (salsa)
let x00 = y00;
let x01 = y01;
let x02 = y02;
let x03 = y03;
let x04 = y04;
let x05 = y05;
let x06 = y06;
let x07 = y07;
let x08 = y08;
let x09 = y09;
let x10 = y10;
let x11 = y11;
let x12 = y12;
let x13 = y13;
let x14 = y14;
let x15 = y15;
// Main loop (salsa)
for (let i = 0; i < 8; i += 2) {
x04 ^= rotl(x00 + x12 | 0, 7);
x08 ^= rotl(x04 + x00 | 0, 9);
x12 ^= rotl(x08 + x04 | 0, 13);
x00 ^= rotl(x12 + x08 | 0, 18);
x09 ^= rotl(x05 + x01 | 0, 7);
x13 ^= rotl(x09 + x05 | 0, 9);
x01 ^= rotl(x13 + x09 | 0, 13);
x05 ^= rotl(x01 + x13 | 0, 18);
x14 ^= rotl(x10 + x06 | 0, 7);
x02 ^= rotl(x14 + x10 | 0, 9);
x06 ^= rotl(x02 + x14 | 0, 13);
x10 ^= rotl(x06 + x02 | 0, 18);
x03 ^= rotl(x15 + x11 | 0, 7);
x07 ^= rotl(x03 + x15 | 0, 9);
x11 ^= rotl(x07 + x03 | 0, 13);
x15 ^= rotl(x11 + x07 | 0, 18);
x01 ^= rotl(x00 + x03 | 0, 7);
x02 ^= rotl(x01 + x00 | 0, 9);
x03 ^= rotl(x02 + x01 | 0, 13);
x00 ^= rotl(x03 + x02 | 0, 18);
x06 ^= rotl(x05 + x04 | 0, 7);
x07 ^= rotl(x06 + x05 | 0, 9);
x04 ^= rotl(x07 + x06 | 0, 13);
x05 ^= rotl(x04 + x07 | 0, 18);
x11 ^= rotl(x10 + x09 | 0, 7);
x08 ^= rotl(x11 + x10 | 0, 9);
x09 ^= rotl(x08 + x11 | 0, 13);
x10 ^= rotl(x09 + x08 | 0, 18);
x12 ^= rotl(x15 + x14 | 0, 7);
x13 ^= rotl(x12 + x15 | 0, 9);
x14 ^= rotl(x13 + x12 | 0, 13);
x15 ^= rotl(x14 + x13 | 0, 18);
}
// Write output (salsa)
output[oi++] = (y00 + x00) | 0;
output[oi++] = (y01 + x01) | 0;
output[oi++] = (y02 + x02) | 0;
output[oi++] = (y03 + x03) | 0;
output[oi++] = (y04 + x04) | 0;
output[oi++] = (y05 + x05) | 0;
output[oi++] = (y06 + x06) | 0;
output[oi++] = (y07 + x07) | 0;
output[oi++] = (y08 + x08) | 0;
output[oi++] = (y09 + x09) | 0;
output[oi++] = (y10 + x10) | 0;
output[oi++] = (y11 + x11) | 0;
output[oi++] = (y12 + x12) | 0;
output[oi++] = (y13 + x13) | 0;
output[oi++] = (y14 + x14) | 0;
output[oi++] = (y15 + x15) | 0;
}
function block_mix(input, input_index, output, output_index, r) {
let head = output_index;
let tail = output_index + (r << 4);
const t = ((r << 1) - 1) << 4;
for (let i = 0; i < 16; i++)
output[tail + i] = input[input_index + i + t];
for (let i = 0; i < r; i++) {
fast_xor_salsa_hash(output, tail, input, input_index, output, head);
if (i > 0) {
tail += 16;
}
input_index += 16;
fast_xor_salsa_hash(output, head, input, input_index, output, tail);
head += 16;
input_index += 16;
}
}
return (k_byte, ikm, salt = new Uint8Array(0)) => {
const B = kdf(BLOCK_SIZE * p, ikm, salt);
const B32 = u32(B);
const V32 = u32(new Uint8Array(BLOCK_SIZE * N));
const tmp = u32(new Uint8Array(BLOCK_SIZE));
for (let pi = 0; pi < p; pi++) {
const PI = BLOCK_SIZE_32 * pi;
V32.set(B32.subarray(PI, PI + BLOCK_SIZE_32), 0);
let pos = 0;
for (let i = 0; i < N_1; i++) {
block_mix(V32, pos, V32, pos + BLOCK_SIZE_32, r);
pos += BLOCK_SIZE_32;
}
block_mix(V32, N_1 * BLOCK_SIZE_32, B32, PI, r);
for (let i = 0; i < N; i++) {
const j = B32[PI + BLOCK_SIZE_32 - 16] % N;
for (let k = 0; k < BLOCK_SIZE_32; k++) {
tmp[k] = B32[PI + k] ^ V32[j * BLOCK_SIZE_32 + k];
}
block_mix(tmp, 0, B32, PI, r);
}
}
return kdf(k_byte, ikm, B);
};
}

@@ -1,2 +0,2 @@

import { U8, modPow } from './utils';
import { modPow, U8 } from './utils';
// * Constants

@@ -3,0 +3,0 @@ /** deterministic >= 1 - 0.5^t */

// * Math utility functions
/** 32-bit 循环左移 */
export function rotateL32(x, n) {
x >>>= 0;
n %= 32;
x = (x << n) | (x >>> (32 - n));
return x >>> 0;
return ((x << n) | (x >>> (32 - n))) >>> 0;
}
/** 32-bit 循环右移 */
export function rotateR32(x, n) {
x >>>= 0;
n %= 32;
x = (x >>> n) | (x << (32 - n));
return x >>> 0;
return (x >>> n) | (x << (32 - n)) >>> 0;
}

@@ -380,2 +374,26 @@ /**

/**
* Convert TypedArray to Uint8Array
*
* 将 TypedArray 转换为 Uint8Array
*/
export function u8(source) {
return new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
}
/**
* Convert TypedArray to Uint16Array
*
* 将 TypedArray 转换为 Uint16Array
*/
export function u16(source) {
return new Uint16Array(source.buffer, source.byteOffset, source.byteLength >> 1);
}
/**
* Convert TypedArray to Uint32Array
*
* 将 TypedArray 转换为 Uint32Array
*/
export function u32(source) {
return new Uint32Array(source.buffer, source.byteOffset, source.byteLength >> 2);
}
/**
* Merging multiple ArrayBuffers

@@ -457,6 +475,14 @@ *

// * Other utility functions
export function trying(fn) {
try {
const result = fn();
return [null, result];
}
catch (error) {
return error instanceof Error
? [error, null]
: [new KitError('Unknown error'), null];
}
}
export function wrap(...args) {
if (args.length === 0) {
return {};
}
// @ts-expect-error Object assign

@@ -463,0 +489,0 @@ return Object.assign(...args);

@@ -9,6 +9,6 @@ import { U8 } from '../core/utils';

*
* @param {Uint8Array} secret - 密钥
* @param {Uint8Array} counter - 计数器
* @param {KeyHash} mac - 带密钥的加密散列算法
* @returns {U8} - 返回的 HOTP 字节数组
* @param {Uint8Array} secret - 密钥 / Secret key
* @param {Uint8Array} counter - 计数器 / Counter
* @param {KeyHash} mac - 带密钥的加密散列算法 / Keyed Hashing Algorithm (default: HMAC-SHA1)
* @returns {U8} - 返回的 HOTP 字节数组 / HOTP byte array
*/

@@ -15,0 +15,0 @@ function hotp(secret, counter, mac = hmac(sha1)) {

@@ -1,6 +0,54 @@

// * Utils
export { U8, joinBuffer } from './core/utils';
// * Block Cipher
export { aes } from './cipher/blockCipher/aes';
export { arc5 } from './cipher/blockCipher/arc5';
export { aria } from './cipher/blockCipher/aria';
export { blowfish } from './cipher/blockCipher/blowfish';
export { camellia } from './cipher/blockCipher/camellia';
export { des, t_des } from './cipher/blockCipher/des';
export { sm4 } from './cipher/blockCipher/sm4';
export { tea, xtea, xxtea } from './cipher/blockCipher/tea';
export { twofish } from './cipher/blockCipher/twofish';
export { FpECC } from './cipher/pkcs/ecc';
export { pkcs1_es_1_5, pkcs1_es_oaep } from './cipher/pkcs/pkcs1';
export { pkcs1_ssa_1_5, pkcs1_ssa_pss } from './cipher/pkcs/pkcs1';
export { mgf1 } from './cipher/pkcs/pkcs1';
export { rsa } from './cipher/pkcs/rsa';
export { sm2 } from './cipher/pkcs/sm2';
export { x448, x25519 } from './cipher/pkcs/x25519_448';
// * Stream Cipher
export { arc4 } from './cipher/streamCipher/arc4';
export { rabbit } from './cipher/streamCipher/rabbit';
export { salsa20 } from './cipher/streamCipher/salsa20';
export { eea3, eia3, zuc } from './cipher/streamCipher/zuc';
export { createCipher } from './core/cipher';
// * Block Cipher Modes
export { cbc, cfb, ctr, ecb, gcm, ofb, pcbc } from './core/cipher';
// * Block Cipher Padding
export { ISO7816_PAD, NO_PAD, PKCS7_PAD, X923_PAD, ZERO_PAD } from './core/cipher';
export { B32, B64, B64URL, CSV, HEX, UTF8 } from './core/codec';
export { sm2p256v1 } from './core/ecParams';
// TODO 实现 爱德华曲线 后再开放
// export { ed25519, ed448 } from './core/ecParams'
// export { secp112r1, secp112r2 } from './core/ecParams'
// export { secp128r1, secp128r2 } from './core/ecParams'
// export { secp160k1, secp160r1, secp160r2 } from './core/ecParams'
export { secp192k1, secp192r1 } from './core/ecParams';
export { secp224k1, secp224r1 } from './core/ecParams';
export { secp256k1, secp256r1 } from './core/ecParams';
export { secp384r1, secp521r1 } from './core/ecParams';
export { prime192v1, prime256v1 } from './core/ecParams';
export { p192, p224, p256, p384, p521 } from './core/ecParams';
export { w448, w25519 } from './core/ecParams';
export { curve448, curve25519 } from './core/ecParams';
export { bp192r1, bp224r1, bp256r1, bp320r1, bp384r1, bp512r1 } from './core/ecParams';
export { createHash, createTupleHash } from './core/hash';
export { hkdf, pbkdf2, scrypt, x963kdf } from './core/kdf';
export { genPrime, isProbablePrime } from './core/prime';
export { UTF8, HEX, B32, B64, B64URL, CSV } from './core/codec';
export { createHash, createTupleHash } from './core/hash';
export { joinBuffer, U8 } from './core/utils';
// * HMAC
export { hmac } from './hash/hmac';
// * KangarooTwelve
export { kt128, kt256 } from './hash/kangaroo12';
// * SHA-3 Keccak
export { keccak_p_1600, sponge_1600 } from './hash/keccak1600';
// * MD5

@@ -10,7 +58,3 @@ export { md5 } from './hash/md5';

export { sha1 } from './hash/sha1';
// * SHA-2
export { sha224, sha256 } from './hash/sha256';
export { sha384, sha512, sha512t } from './hash/sha512';
// * SHA-3
export { keccak_p_1600, sponge_1600 } from './hash/keccak1600';
export { sha3_224, sha3_256 } from './hash/sha3';

@@ -31,53 +75,10 @@ export { sha3_384, sha3_512 } from './hash/sha3';

export { parallelhash256, parallelhash256XOF } from './hash/sha3Derived';
// * TurboSHAKE
export { turboshake128, turboshake256 } from './hash/turboSHAKE';
// * KangarooTwelve
export { kt128, kt256 } from './hash/kangaroo12';
// * SHA-2
export { sha224, sha256 } from './hash/sha256';
export { sha384, sha512, sha512t } from './hash/sha512';
// * SM3
export { sm3 } from './hash/sm3';
// * HMAC
export { hmac } from './hash/hmac';
// * TOTP
export { totp } from './hash/totp';
export { createCipher } from './core/cipher';
// * Block Cipher
export { sm4 } from './cipher/blockCipher/sm4';
export { aes } from './cipher/blockCipher/aes';
export { aria } from './cipher/blockCipher/aria';
export { camellia } from './cipher/blockCipher/camellia';
export { des, t_des } from './cipher/blockCipher/des';
export { arc5 } from './cipher/blockCipher/arc5';
export { blowfish } from './cipher/blockCipher/blowfish';
export { twofish } from './cipher/blockCipher/twofish';
export { tea, xtea, xxtea } from './cipher/blockCipher/tea';
// * Block Cipher Modes
export { ecb, cbc, pcbc, cfb, ofb, ctr, gcm } from './core/cipher';
// * Block Cipher Padding
export { PKCS7_PAD, ZERO_PAD, X923_PAD, ISO7816_PAD, NO_PAD } from './core/cipher';
export { eea3, eia3, zuc } from './cipher/streamCipher/zuc';
export { arc4 } from './cipher/streamCipher/arc4';
export { salsa20 } from './cipher/streamCipher/salsa20';
export { rabbit } from './cipher/streamCipher/rabbit';
export { rsa } from './cipher/pkcs/rsa';
export { pkcs1_es_1_5, pkcs1_es_oaep } from './cipher/pkcs/pkcs1';
export { pkcs1_ssa_1_5, pkcs1_ssa_pss } from './cipher/pkcs/pkcs1';
export { mgf1 } from './cipher/pkcs/pkcs1';
export { x963kdf, hkdf, pbkdf2 } from './core/kdf';
export { FpECC } from './cipher/pkcs/ecc';
export { sm2p256v1 } from './core/ecParams';
// export { secp112r1, secp112r2 } from './core/ecParams'
// export { secp128r1, secp128r2 } from './core/ecParams'
// export { secp160k1, secp160r1, secp160r2 } from './core/ecParams'
export { secp192k1, secp192r1 } from './core/ecParams';
export { secp224k1, secp224r1 } from './core/ecParams';
export { secp256k1, secp256r1 } from './core/ecParams';
export { secp384r1, secp521r1 } from './core/ecParams';
export { prime192v1, prime256v1 } from './core/ecParams';
export { p192, p224, p256, p384, p521 } from './core/ecParams';
export { w25519, w448 } from './core/ecParams';
// TODO 实现 爱德华曲线 后再开放
// export { ed25519, ed448 } from './core/ecParams'
export { curve25519, curve448 } from './core/ecParams';
export { bp192r1, bp224r1, bp256r1, bp320r1, bp384r1, bp512r1 } from './core/ecParams';
export { sm2 } from './cipher/pkcs/sm2';
export { x25519, x448 } from './cipher/pkcs/x25519_448';
// * TurboSHAKE
export { turboshake128, turboshake256 } from './hash/turboSHAKE';

@@ -0,1 +1,65 @@

/**
* 高级加密标准 (AES) 分组密码算法
*
* Advanced Encryption Standard (AES) block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function aes(b: 128 | 192 | 256): BlockCipher;
/**
* ARC5 分组加密算法 / block cipher algorithm
*
* ```ts
* const spec8 = arc5(8, 8) // ARC5-8/8
* const spec16 = arc5(16, 12) // ARC5-16/12
* const spec32 = arc5(32, 16) // ARC5-32/16 (default)
* const spec64 = arc5(64, 20) // ARC5-64/20
* const spec128 = arc5(128, 24) // ARC5-128/24
* ```
*
* @param {16 | 32 | 64} WORD_SIZE - 工作字长 / Word size (default: 32 bit)
* @param {number} round - 轮数 / Rounds (default: 16)
*/
declare function arc5(WORD_SIZE?: 8 | 16 | 32 | 64 | 128, round?: number): BlockCipher;
/**
* ARIA 分组密码算法 / block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function aria(b: 128 | 192 | 256): BlockCipher;
/**
* Blowfish 分组密码算法 / block cipher algorithm
*/
declare const blowfish: BlockCipher;
/**
* Camellia 分组密码算法 / block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function camellia(b: 128 | 192 | 256): BlockCipher;
/**
* Data Encryption Standard (DES) block cipher algorithm
*
* 数据加密标准(DES)分组密码算法
*/
declare const des: BlockCipher;
/**
* Triple Data Encryption Standard (3DES) block cipher algorithm
*
* 三重数据加密标准(3DES)分组密码算法
*
* @param {128 | 192} l - 密钥长度 / Key Size (bit)
*/
declare function t_des(l: 128 | 192): BlockCipher;
/**
* SM4 分组密码算法 / block cipher algorithm
*/
declare const sm4: BlockCipher;
/** 字符编解码器 / String Codec */

@@ -122,410 +186,2 @@ interface Codec {

/** 随机素数生成器 / Random Prime Generator */
interface RandomPrimeGenerator {
/**
* @param {bigint} b - 位数 / Bits
*/
(b: number): bigint;
}
/** 随机素数生成器 / Random Prime Generator */
declare const genPrime: RandomPrimeGenerator;
/**
* 素性测试: 确定性 >= 1-.5^t
*
* Primality test: deterministic >= 1-.5^t
*
* @param {bigint} n - 待测试的数 / Number to be tested
* @param {number} t - 测试轮数 / Number of tests
*/
declare function isProbablePrime(n: bigint, t?: number): boolean;
interface Digest {
/**
* @param {Uint8Array} M - 消息 / message
*/
(M: Uint8Array): U8;
}
interface HashDescription {
/** 算法名称 / Algorithm name */
ALGORITHM: string;
/** 分块大小 / Block size (byte) */
BLOCK_SIZE: number;
/** 摘要大小 / Digest size (byte) */
DIGEST_SIZE: number;
OID?: string;
}
interface Hash extends Digest, HashDescription {
}
/**
* 散列算法包装器,
* 提供散列算法描述, 以实现 `HMAC` 等拓展算法.
*
* Hash algorithm wrapper,
* provide hash algorithm description to implement extended algorithms such as `HMAC`.
*
* @param {Digest} digest - 摘要函数 / digest function
* @param {HashDescription} description - 算法描述 / algorithm description
*
* ```ts
* const digest: Digest = (M: Uint8Array): U8 => { ... }
* const description: HashDescription = { ... }
* const hash = createHash(digest, description)
* ```
*/
declare const createHash: (digest: Digest, description: HashDescription) => Hash;
interface TupleDigest {
/**
* @param {Uint8Array[]} M - 消息 / message
*/
(M: Uint8Array[]): U8;
}
interface TupleHashDescription extends HashDescription {
}
interface TupleHash extends TupleDigest, TupleHashDescription {
}
/**
* 元组散列算法包装器
*
* Tuple hash algorithm wrapper
*
* @param {TupleDigest} digest - 元组摘要函数 / tuple digest function
* @param {TupleHashDescription} description - 算法描述 / algorithm description
*
* ```ts
* const digest: TupleDigest = (M: Uint8Array[]): U8 => { ... }
* const description: TupleHashDescription = { ... }
* const hash = createTupleHash(digest, description)
* ```
*/
declare const createTupleHash: (digest: TupleDigest, description: TupleHashDescription) => TupleHash;
interface KeyDigest {
/**
* @param {Uint8Array} K - 密钥 / key
* @param {Uint8Array} M - 消息 / message
*/
(K: Uint8Array, M: Uint8Array): U8;
}
interface KeyHashDescription extends HashDescription {
/** 推荐的密钥大小 / Recommended key size (byte) */
KEY_SIZE: number;
}
/** 密钥散列函数 / Keyed hash function */
interface KeyHash extends KeyDigest, KeyHashDescription {
}
declare const md5: Hash;
declare const sha1: Hash;
declare const sha224: Hash;
declare const sha256: Hash;
declare const sha384: Hash;
declare const sha512: Hash;
/**
* @param {number} t - 截断长度 / truncation length (bit)
*/
declare function sha512t(t: number): Hash;
/**
* `Keccak-p` 置换函数 / Permutate Function
*/
interface Keccak_p {
/**
* @param {Uint8Array} S - 状态 / State
*/
(S: Uint8Array): Uint8Array;
}
/**
* `Keccak-p[1600, nr]` 置换函数 / Permutate Function
*
* @param {number} [nr] - 轮数 / Rounds (default: 24)
*/
declare function keccak_p_1600(nr?: number): Keccak_p;
/**
* `SPONGE` 填充函数 / Padding Function
*/
interface SpongePadding {
/**
* @param {Uint8Array} M - 消息 / Message
*/
(M: Uint8Array): Uint8Array;
}
/**
* `SPONGE` & `Keccak-p[1600]`
*
* @param {number} r_byte - 处理速率 / Rate
* @param {number} d_byte - 输出长度 / Digest Size
* @param {SpongePadding} pad - 填充函数 / Padding Function
* @param {Keccak_p} f - Keccak-p 置换函数 / Permutate Function
*/
declare function sponge_1600(r_byte: number, d_byte: number, pad: SpongePadding, f?: Keccak_p): (M: Uint8Array) => U8;
declare const sha3_224: Hash;
declare const sha3_256: Hash;
declare const sha3_384: Hash;
declare const sha3_512: Hash;
/**
* @param {number} d - 输出长度 / Digest Size (bit)
*/
declare function shake128(d: number): Hash;
/**
* @param {number} d - 输出长度 / Digest Size (bit)
*/
declare function shake256(d: number): Hash;
/**
* `cSHAKE128` 是 `SHAKE128` 的可定制变体
*
* `cSHAKE128` is a customizable variant of `SHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [N] - 函数名 / Function name
* @param {Uint8Array} [S] - 自定义参数 / Customization
*/
declare function cshake128(d: number, N?: Uint8Array<ArrayBuffer>, S?: Uint8Array<ArrayBuffer>): Hash;
/**
* `cSHAKE256` 是 `SHAKE256` 的可定制变体
*
* `cSHAKE256` is a customizable variant of `SHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [N] - 函数名 / Function name
* @param {Uint8Array} [S] - 自定义参数 / Customization
*/
declare function cshake256(d: number, N?: Uint8Array<ArrayBuffer>, S?: Uint8Array<ArrayBuffer>): Hash;
/**
* Keccak 消息认证码 (KMAC) 算法
* `KMAC128` 是 `KMAC` 的变体, 由 `cSHAKE128` 构建
*
* The Keccak Message Authentication Code (KMAC) algorithm
* `KMAC128` is a variant of `KMAC`, build from `cSHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac128(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* Keccak 消息认证码 (KMAC) 算法
* `KMAC256` 是 `KMAC` 的变体, 由 `cSHAKE256` 构建
*
* The Keccak Message Authentication Code (KMAC) algorithm
* `KMAC256` is a variant of `KMAC`, build from `cSHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac256(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* 可变长度输出的 `KMAC`
* `KMAC128XOF` 是 `KMAC128` 的 XOF 模式, 由 `cSHAKE128` 构建
*
* `KMAC` with Arbitrary-Length Output
* `KMAC128XOF` is a XOF mode of `KMAC128`, build from `cSHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac128XOF(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* 可变长度输出的 `KMAC`
* `KMAC256XOF` 是 `KMAC256` 的 XOF 模式, 由 `cSHAKE256` 构建
*
* `KMAC` with Arbitrary-Length Output
* `KMAC256XOF` is a XOF mode of `KMAC256`, build from `cSHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / recommended key size (bit)
*/
declare function kmac256XOF(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* `TupleHash` 是一个具有可变长度输出的 `SHA3` 派生散列函数, 旨在以一种明确的方式简单地散列输入字符串的元组, 这些字符串中的任何一个或全部都可以是空字符串.
*
* `TupleHash` is a `SHA3` derived hash function with variable-length output that is designed to simply hash a tuple of input strings, any or all of which may be empty strings, in an unambiguous way.
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash128(d: number, S?: Uint8Array): TupleHash;
/**
* `TupleHash` 是一个具有可变长度输出的 `SHA3` 派生散列函数, 旨在以一种明确的方式简单地散列输入字符串的元组, 这些字符串中的任何一个或全部都可以是空字符串.
*
* `TupleHash` is a `SHA3` derived hash function with variable-length output that is designed to simply hash a tuple of input strings, any or all of which may be empty strings, in an unambiguous way.
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash256(d: number, S?: Uint8Array): TupleHash;
/**
* 可变长度输出的 `TupleHash`
*
* `TupleHash` with Arbitrary-Length Output
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash128XOF(d: number, S?: Uint8Array): TupleHash;
/**
* 可变长度输出的 `TupleHash`
*
* `TupleHash` with Arbitrary-Length Output
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash256XOF(d: number, S?: Uint8Array): TupleHash;
/**
* `ParallelHash` 的目的是利用现代处理器中可用的并行性, 支持对非常长的字符串进行高效散列.
*
* The purpose of `ParallelHash` is to support the efficient hashing of very long strings, by taking advantage of the parallelism available in modern processors.
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash128(b: number, d: number, S?: Uint8Array): Hash;
/**
* `ParallelHash` 的目的是利用现代处理器中可用的并行性, 支持对非常长的字符串进行高效散列.
*
* The purpose of `ParallelHash` is to support the efficient hashing of very long strings, by taking advantage of the parallelism available in modern processors.
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash256(b: number, d: number, S?: Uint8Array): Hash;
/**
* 可变长度输出的 `ParallelHash`
*
* `ParallelHash` with Arbitrary-Length Output
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash128XOF(b: number, d: number, S?: Uint8Array): Hash;
/**
* 可变长度输出的 `ParallelHash`
*
* `ParallelHash` with Arbitrary-Length Output
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash256XOF(b: number, d: number, S?: Uint8Array): Hash;
/**
* TurboSHAKE128
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {number} [D] - 域分隔符 / Domain Separator (range: 0x01 ~ 0x7F, default: 0x1F)
*/
declare function turboshake128(d: number, D?: number): Hash;
/**
* TurboSHAKE256
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {number} [D] - 域分隔符 / Domain Separator (range: 0x01 ~ 0x7F, default: 0x1F)
*/
declare function turboshake256(d: number, D?: number): Hash;
/**
* KangarooTwelve 128
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [C] - 自定义参数 / Customization
*/
declare function kt128(d: number, C?: Uint8Array<ArrayBuffer>): Hash;
/**
* KangarooTwelve 256
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [C] - 自定义参数 / Customization
*/
declare function kt256(d: number, C?: Uint8Array<ArrayBuffer>): Hash;
declare const sm3: Hash;
/**
* FIPS.198-1: 散列消息认证码 (HMAC).
* 如果 `d_size` 大于散列算法的摘要大小, 则回退到散列算法的摘要大小.
*
* FIPS.198-1: The Keyed-Hash Message Authentication Code (HMAC).
* If `d_size` is larger than the hash algorithm's digest size, fallback to the hash algorithm's digest size.
*
* @param {Hash} hash - 散列算法 / hash algorithm
* @param {number} [d_size] - 摘要大小 (bit) / digest size (bit)
* @param {number} [k_size] - 推荐密钥大小 (bit) / recommended key size (bit)
*/
declare function hmac(hash: Hash, d_size?: number, k_size?: number): KeyHash;
interface TOTP {
/**
* 生成 TOTP (时间同步的一次性密码)
*
* Generate TOTP (Time-based One-Time Password)
*
* @param {Uint8Array} secret - 密钥
* @returns {string} - 返回的 TOTP 字符串
*/
(secret: Uint8Array): string;
}
interface TOTPParams {
/**
* 带密钥的加密散列算法 / Keyed Hashing Algorithm (default: HMAC-SHA1)
*/
mac?: KeyHash;
/**
* 当前时间戳 / Current timestamp (default: Date.now() milliseconds)
*
* 指定此参数时,将不再从 `Date.now()` 获取当前时间戳.
*
* When this parameter is specified, the current timestamp will not be obtained from `Date.now()`.
*/
current?: number;
/**
* 纪元时间戳 / Epoch timestamp (default: 0 milliseconds)
*/
epoch?: number;
/**
* 时间步长 / Time step (default: 30000 milliseconds)
*/
step?: number;
/**
* 计数器 / Counter
*
* `counter = (cuttent_time - epoch_time) / step`
*
* 指定此参数时,将不再从当前时间戳计算计数器.
*
* When this parameter is specified, the counter will not be calculated from the current timestamp.
*/
counter?: number | bigint | Uint8Array;
/**
* 返回的数字位数 / Number of digits in the returned OTP (default: 6)
*/
digits?: number;
}
/**
* 生成 TOTP (时间同步的一次性密码)
*
* Generate TOTP (Time-based One-Time Password)
*
* @param {Uint8Array} secret - 密钥
* @returns {string} - 返回的 TOTP 字符串
*/
declare function totp(secret: Uint8Array): string;
/**
* 创建 TOTP 函数 / Create a TOTP function
*
* @param {TOTPParams} params - TOTP 参数
* @returns {TOTP} - 返回的 TOTP 函数
*/
declare function totp(params: TOTPParams): TOTP;
interface Cipherable {

@@ -736,73 +392,2 @@ /**

/**
* SM4 分组密码算法 / block cipher algorithm
*/
declare const sm4: BlockCipher;
/**
* 高级加密标准 (AES) 分组密码算法
*
* Advanced Encryption Standard (AES) block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function aes(b: 128 | 192 | 256): BlockCipher;
/**
* ARIA 分组密码算法 / block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function aria(b: 128 | 192 | 256): BlockCipher;
/**
* Camellia 分组密码算法 / block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function camellia(b: 128 | 192 | 256): BlockCipher;
/**
* Data Encryption Standard (DES) block cipher algorithm
*
* 数据加密标准(DES)分组密码算法
*/
declare const des: BlockCipher;
/**
* Triple Data Encryption Standard (3DES) block cipher algorithm
*
* 三重数据加密标准(3DES)分组密码算法
*
* @param {128 | 192} l - 密钥长度 / Key Size (bit)
*/
declare function t_des(l: 128 | 192): BlockCipher;
/**
* ARC5 分组加密算法 / block cipher algorithm
*
* ```ts
* const spec8 = arc5(8, 8) // ARC5-8/8
* const spec16 = arc5(16, 12) // ARC5-16/12
* const spec32 = arc5(32, 16) // ARC5-32/16 (default)
* const spec64 = arc5(64, 20) // ARC5-64/20
* const spec128 = arc5(128, 24) // ARC5-128/24
* ```
*
* @param {16 | 32 | 64} WORD_SIZE - 工作字长 / Word size (default: 32 bit)
* @param {number} round - 轮数 / Rounds (default: 16)
*/
declare function arc5(WORD_SIZE?: 8 | 16 | 32 | 64 | 128, round?: number): BlockCipher;
/**
* Blowfish 分组密码算法 / block cipher algorithm
*/
declare const blowfish: BlockCipher;
/**
* Twofish 分组密码算法 / block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function twofish(b: 128 | 192 | 256): BlockCipher;
/**
* 微型加密算法 (TEA) 分组密码算法

@@ -864,6 +449,2 @@ *

* Corrected Block TEA (XXTEA) block cipher algorithm
*
* @param {Padding} [config.padding] - 填充方式 / Padding method (default: PKCS7)
* @param {number} [config.round] - 轮数 / Rounds (default: undefined)
* @param {number} [config.BLOCK_SIZE] - 分组大小 / Block size (default: 16)
*/

@@ -873,224 +454,9 @@ declare function xxtea(config?: XXTEAConfig): BlockCipher;

/**
* 3GPP ZUC 算法用于生成密钥流,每次调用返回一个 32 位的密钥流.
* Twofish 分组密码算法 / block cipher algorithm
*
* 3GPP ZUC algorithm is used to generate a key stream, each call returns a 32-bit key stream.
*
* ```ts
* const K = new Uint8Array(16)
* const iv = new Uint8Array(16)
* const prg = zuc(K, iv)
* prg() // 32-bit number
* ```
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function zuc(K: Uint8Array, iv: Uint8Array): () => number;
interface ZUCParams {
/**
* 32-bit counter
*
* if `counter` is `number` type, convert to `Uint8Array` type in little-endian.
*
* 如果 `counter` 为 `number` 类型,则转换为小端存储的 `Uint8Array` 类型.
*/
COUNTER: Uint8Array | number;
/**
* 5-bit bearer
*/
BEARER: number;
/**
* 1-bit direction
*/
DIRECTION: 0 | 1;
/**
* 128-bit key
*/
KEY: Uint8Array;
/**
* 32-bit length
*/
LENGTH: number;
M: Uint8Array;
}
interface ZUC3GPP {
(param: ZUCParams): U8;
}
/**
* 3GPP ZUC 加密算法 / Encryption algorithm
*/
declare const eea3: ZUC3GPP;
/**
* 3GPP ZUC 完整性算法 / Integrity algorithm
*/
declare const eia3: ZUC3GPP;
declare function twofish(b: 128 | 192 | 256): BlockCipher;
/**
* ARC4 流密码 / stream cipher
*/
declare const arc4: StreamCipher;
/**
* Salsa20 流密码 / Stream Cipher
*/
declare const salsa20: IVStreamCipher;
/**
* Rabbit 流密码 / stream cipher
*/
declare const rabbit: IVStreamCipher;
interface RSAPublicKey {
/** 模数 / Modulus */
n: bigint;
/** 公钥指数 / Public Exponent */
e: bigint;
}
interface RSAPrivateKey extends RSAPublicKey {
/** 模数 / Modulus */
n: bigint;
/** 公钥指数 / Public Exponent */
e: bigint;
/** 私钥指数 / Private Exponent */
d: bigint;
p: bigint;
q: bigint;
dP: bigint;
dQ: bigint;
qInv: bigint;
}
interface RSACipherable {
/**
* 使用 RSA 加密原语加密消息
*
* Encrypt message using RSA encryption primitive
*/
encrypt: (M: Uint8Array) => bigint;
/**
* 使用 RSA 解密原语解密密文
*
* Decrypt ciphertext using RSA decryption primitive
*/
decrypt: (C: Uint8Array) => bigint;
}
interface RSAVerifiable {
/**
* 使用 RSA 签名原语对消息签名
*
* Sign message using RSA signature primitive
*/
sign: (M: Uint8Array) => bigint;
/**
* 使用 RSA 验证原语验证签名
*
* Verify signature using RSA verification primitive
*/
verify: (S: Uint8Array) => bigint;
}
/**
* 根据 RSA 私钥长度生成 RSA 密钥对, 并返回 RSA 加密原语和签名原语
*
* Generate RSA key pair according to RSA private key length, and return RSA encryption primitive and signature primitive
*
* @param {number} b - RSA 私钥长度 / RSA private key length
* @param {RandomPrimeGenerator} rpg - 随机素数生成器 / Random prime generator
*/
declare function rsa(b: number, rpg?: RandomPrimeGenerator): RSACipherable & RSAVerifiable & RSAPrivateKey;
/**
* 根据 RSA 公钥或私钥生成 RSA 加密原语和验证原语
*
* Generate RSA encryption primitive and verification primitive according to RSA public or private key
*
* @param {RSAPrivateKey | RSAPublicKey} key - RSA 公钥或私钥 / RSA public or private key
*/
declare function rsa<T extends RSAPrivateKey | RSAPublicKey>(key: T): RSACipherable & RSAVerifiable & T;
interface MGF {
(mdfSeed: Uint8Array, maskLen: number): Uint8Array;
}
/**
* PKCS#1 v2.2 的 掩码生成函数 MGF1
*
* Mask Generation Function MGF1 of PKCS#1 v2.2
*/
declare function mgf1(hash: Hash): MGF;
/**
* 最优非对称加密填充的 RSA 加密方案 (OAEP)
*
* RSA Encryption Scheme with Optimal Asymmetric Encryption Padding (OAEP)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
* @param {MGF} [mgf] - 掩码生成函数 / Mask generation function (default: MGF1)
* @param {Uint8Array} [label] - 标签 / Label (default: empty)
*/
declare function pkcs1_es_oaep(key: RSAPublicKey | RSAPrivateKey, hash?: Hash, mgf?: MGF, label?: Uint8Array<ArrayBuffer>): {
encrypt: (M: Uint8Array) => U8;
decrypt: (C: Uint8Array) => U8;
};
/**
* RSA 加密方案 (PKCS#1 v1.5)
*
* RSA Encryption Scheme (PKCS#1 v1.5)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
*/
declare function pkcs1_es_1_5(key: RSAPublicKey | RSAPrivateKey): {
encrypt: (M: Uint8Array) => U8;
decrypt: (C: Uint8Array) => U8;
};
/**
* 基于 概率签名方案 的 RSA 附录签名方案 (PSS)
*
* RSA Signature Scheme with Appendix - Probabilistic Signature Scheme (PSS)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
* @param {MGF} [mgf] - 掩码生成函数 / Mask generation function (default: MGF1)
* @param {number} [sLen] - 盐长度 / Salt length (default: hash.DIGEST_SIZE)
*/
declare function pkcs1_ssa_pss(key: RSAPublicKey | RSAPrivateKey, hash?: Hash, mgf?: MGF, sLen?: number): {
sign: (M: Uint8Array) => U8;
verify: (M: Uint8Array, S: Uint8Array) => boolean;
};
/**
* RSA 附录签名方案 (PKCS#1 v1.5)
*
* RSA Signature Scheme with Appendix (PKCS#1 v1.5)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
*/
declare function pkcs1_ssa_1_5(key: RSAPublicKey | RSAPrivateKey, hash?: Hash): {
sign: (M: Uint8Array) => U8;
verify: (M: Uint8Array, S: Uint8Array) => boolean;
};
interface KDF {
/**
* @param {number} k_bit - 期望的密钥长度 / output keying material length
* @param {Uint8Array} ikm - 输入密钥材料 / input keying material
* @param {Uint8Array} info - 附加信息 / optional context and application specific information
*/
(k_bit: number, ikm: Uint8Array, info?: Uint8Array): U8;
}
/**
* ANSI-X9.63 Key Derivation Function
*
* ANSI-X9.63 密钥派生函数
*/
declare function x963kdf(hash: Hash): KDF;
/**
* HMAC-based Key Derivation Function (HKDF), please combine `hmac` and `hash` externally to control the behavior of calling `hmac` inside the function.
*
* 基于 HMAC 的密钥派生函数 (HKDF), 请在外部组合 `hmac` 和 `hash` 函数, 以控制在函数内部调用 `hmac` 时的行为.
*/
declare function hkdf(k_hash: KeyHash, salt?: Uint8Array<ArrayBuffer>): KDF;
/**
* Password-Based Key Derivation Function 2 (PBKDF2), please combine `hmac` and `hash` externally to control the behavior of calling `hmac` inside the function.
* Also, PBKDF2 does not use the `info` parameter, if provided, it will be ignored.
*
* PBKDF2 密码基础密钥派生函数 (PBKDF2), 请在外部组合 `hmac` 和 `hash` 函数, 以控制在函数内部调用 `hmac` 时的行为.
* 同时, PBKDF2 不使用 `info` 参数, 如果提供 `info`, 将被忽略.
*/
declare function pbkdf2(k_hash: KeyHash, salt?: Uint8Array<ArrayBuffer>, iterations?: number): KDF;
/**
* 伪射坐标表示的椭圆曲线的点

@@ -1359,2 +725,155 @@ *

interface Digest {
/**
* @param {Uint8Array} M - 消息 / message
*/
(M: Uint8Array): U8;
}
interface HashDescription {
/** 算法名称 / Algorithm name */
ALGORITHM: string;
/** 分块大小 / Block size (byte) */
BLOCK_SIZE: number;
/** 摘要大小 / Digest size (byte) */
DIGEST_SIZE: number;
OID?: string;
}
interface Hash extends Digest, HashDescription {
}
/**
* 散列算法包装器,
* 提供散列算法描述, 以实现 `HMAC` 等拓展算法.
*
* Hash algorithm wrapper,
* provide hash algorithm description to implement extended algorithms such as `HMAC`.
*
* @param {Digest} digest - 摘要函数 / digest function
* @param {HashDescription} description - 算法描述 / algorithm description
*
* ```ts
* const digest: Digest = (M: Uint8Array): U8 => { ... }
* const description: HashDescription = { ... }
* const hash = createHash(digest, description)
* ```
*/
declare const createHash: (digest: Digest, description: HashDescription) => Hash;
interface TupleDigest {
/**
* @param {Uint8Array[]} M - 消息 / message
*/
(M: Uint8Array[]): U8;
}
interface TupleHashDescription extends HashDescription {
}
interface TupleHash extends TupleDigest, TupleHashDescription {
}
/**
* 元组散列算法包装器
*
* Tuple hash algorithm wrapper
*
* @param {TupleDigest} digest - 元组摘要函数 / tuple digest function
* @param {TupleHashDescription} description - 算法描述 / algorithm description
*
* ```ts
* const digest: TupleDigest = (M: Uint8Array[]): U8 => { ... }
* const description: TupleHashDescription = { ... }
* const hash = createTupleHash(digest, description)
* ```
*/
declare const createTupleHash: (digest: TupleDigest, description: TupleHashDescription) => TupleHash;
interface KeyDigest {
/**
* @param {Uint8Array} K - 密钥 / key
* @param {Uint8Array} M - 消息 / message
*/
(K: Uint8Array, M: Uint8Array): U8;
}
interface KeyHashDescription extends HashDescription {
/** 推荐的密钥大小 / Recommended key size (byte) */
KEY_SIZE: number;
}
/** 密钥散列函数 / Keyed hash function */
interface KeyHash extends KeyDigest, KeyHashDescription {
}
interface KDF {
/**
* @param {number} k_byte - 期望的密钥长度 / output keying material length
* @param {Uint8Array} ikm - 输入密钥材料 / input keying material
* @param {Uint8Array} salt - 盐 / salt value
*/
(k_byte: number, ikm: Uint8Array, salt?: Uint8Array): U8;
}
/**
* ANSI-X9.63 Key Derivation Function
*
* ANSI-X9.63 密钥派生函数
*/
declare function x963kdf(hash: Hash, info?: Uint8Array<ArrayBuffer>): KDF;
/**
* HMAC-based Key Derivation Function (HKDF), please combine `hmac` and `hash` externally to control the behavior of calling `hmac` inside the function.
*
* 基于 HMAC 的密钥派生函数 (HKDF), 请在外部组合 `hmac` 和 `hash` 函数, 以控制在函数内部调用 `hmac` 时的行为.
*/
declare function hkdf(k_hash: KeyHash, info?: Uint8Array<ArrayBuffer>): KDF;
/**
* Password-Based Key Derivation Function 2 (PBKDF2), please combine `hmac` and `hash` externally to control the behavior of calling `hmac` inside the function.
*
* PBKDF2 密码基础密钥派生函数 (PBKDF2), 请在外部组合 `hmac` 和 `hash` 函数, 以控制在函数内部调用 `hmac` 时的行为.
*/
declare function pbkdf2(k_hash: KeyHash, iterations?: number): KDF;
interface ScryptConfig {
/**
* 开销因子 / Cost factor (default: 16384)
*
* 必须是 2 的幂
*
* Must be a power of 2
*/
N?: number;
/**
* 块数 / Block count (default: 8)
*/
r?: number;
/**
* 并行因子 / Parallelization factor (default: 1)
*/
p?: number;
/**
* 最大内存使用量 / Maximum memory usage
*
* 如果设置为 0,则不限制内存使用量
*
* If set to 0, there is no limit on memory usage
*
* (default: 0x40000400 bytes, 1GB + 1KB)
*/
maxmem?: number;
/**
* 密钥派生函数 / Key Derivation Function
*
* scrypt 标准使用了 `PBKDF2-HMAC-SHA256` 作为 KDF。
* 该参数允许用户指定其他 KDF,改变 scrypt 的内部行为。
*
* 注意: 这不是 `scrypt` 的标准用法且缺乏相关的安全分析。
*
* The scrypt standard uses `PBKDF2-HMAC-SHA256` as the KDF.
* This parameter allows users to specify a different KDF, changing the internal behavior of scrypt.
*
* Note: This is not the standard usage of `scrypt` and lacks relevant security analysis.
*
* (default: pbkdf2(hmac(sha256), 1))
*/
kdf?: KDF;
}
/**
* Scrypt Key Derivation Function
*
* Scrypt 密钥派生函数
*
* Based on https://github.com/paulmillr/noble-hashes
*/
declare function scrypt(config?: ScryptConfig): KDF;
interface ECPublicKey<T = bigint | Uint8Array> {

@@ -1549,2 +1068,147 @@ /** 椭圆曲线公钥 / Elliptic Curve Public Key */

/** 随机素数生成器 / Random Prime Generator */
interface RandomPrimeGenerator {
/**
* @param {bigint} b - 位数 / Bits
*/
(b: number): bigint;
}
/** 随机素数生成器 / Random Prime Generator */
declare const genPrime: RandomPrimeGenerator;
/**
* 素性测试: 确定性 >= 1-.5^t
*
* Primality test: deterministic >= 1-.5^t
*
* @param {bigint} n - 待测试的数 / Number to be tested
* @param {number} t - 测试轮数 / Number of tests
*/
declare function isProbablePrime(n: bigint, t?: number): boolean;
interface RSAPublicKey {
/** 模数 / Modulus */
n: bigint;
/** 公钥指数 / Public Exponent */
e: bigint;
}
interface RSAPrivateKey extends RSAPublicKey {
/** 模数 / Modulus */
n: bigint;
/** 公钥指数 / Public Exponent */
e: bigint;
/** 私钥指数 / Private Exponent */
d: bigint;
p: bigint;
q: bigint;
dP: bigint;
dQ: bigint;
qInv: bigint;
}
interface RSACipherable {
/**
* 使用 RSA 加密原语加密消息
*
* Encrypt message using RSA encryption primitive
*/
encrypt: (M: Uint8Array) => bigint;
/**
* 使用 RSA 解密原语解密密文
*
* Decrypt ciphertext using RSA decryption primitive
*/
decrypt: (C: Uint8Array) => bigint;
}
interface RSAVerifiable {
/**
* 使用 RSA 签名原语对消息签名
*
* Sign message using RSA signature primitive
*/
sign: (M: Uint8Array) => bigint;
/**
* 使用 RSA 验证原语验证签名
*
* Verify signature using RSA verification primitive
*/
verify: (S: Uint8Array) => bigint;
}
/**
* 根据 RSA 私钥长度生成 RSA 密钥对, 并返回 RSA 加密原语和签名原语
*
* Generate RSA key pair according to RSA private key length, and return RSA encryption primitive and signature primitive
*
* @param {number} b - RSA 私钥长度 / RSA private key length
* @param {RandomPrimeGenerator} rpg - 随机素数生成器 / Random prime generator
*/
declare function rsa(b: number, rpg?: RandomPrimeGenerator): RSACipherable & RSAVerifiable & RSAPrivateKey;
/**
* 根据 RSA 公钥或私钥生成 RSA 加密原语和验证原语
*
* Generate RSA encryption primitive and verification primitive according to RSA public or private key
*
* @param {RSAPrivateKey | RSAPublicKey} key - RSA 公钥或私钥 / RSA public or private key
*/
declare function rsa<T extends RSAPrivateKey | RSAPublicKey>(key: T): RSACipherable & RSAVerifiable & T;
interface MGF {
(mdfSeed: Uint8Array, maskLen: number): Uint8Array;
}
/**
* PKCS#1 v2.2 的 掩码生成函数 MGF1
*
* Mask Generation Function MGF1 of PKCS#1 v2.2
*/
declare function mgf1(hash: Hash): MGF;
/**
* 最优非对称加密填充的 RSA 加密方案 (OAEP)
*
* RSA Encryption Scheme with Optimal Asymmetric Encryption Padding (OAEP)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
* @param {MGF} [mgf] - 掩码生成函数 / Mask generation function (default: MGF1)
* @param {Uint8Array} [label] - 标签 / Label (default: empty)
*/
declare function pkcs1_es_oaep(key: RSAPublicKey | RSAPrivateKey, hash?: Hash, mgf?: MGF, label?: Uint8Array<ArrayBuffer>): {
encrypt: (M: Uint8Array) => U8;
decrypt: (C: Uint8Array) => U8;
};
/**
* RSA 加密方案 (PKCS#1 v1.5)
*
* RSA Encryption Scheme (PKCS#1 v1.5)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
*/
declare function pkcs1_es_1_5(key: RSAPublicKey | RSAPrivateKey): {
encrypt: (M: Uint8Array) => U8;
decrypt: (C: Uint8Array) => U8;
};
/**
* 基于 概率签名方案 的 RSA 附录签名方案 (PSS)
*
* RSA Signature Scheme with Appendix - Probabilistic Signature Scheme (PSS)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
* @param {MGF} [mgf] - 掩码生成函数 / Mask generation function (default: MGF1)
* @param {number} [sLen] - 盐长度 / Salt length (default: hash.DIGEST_SIZE)
*/
declare function pkcs1_ssa_pss(key: RSAPublicKey | RSAPrivateKey, hash?: Hash, mgf?: MGF, sLen?: number): {
sign: (M: Uint8Array) => U8;
verify: (M: Uint8Array, S: Uint8Array) => boolean;
};
/**
* RSA 附录签名方案 (PKCS#1 v1.5)
*
* RSA Signature Scheme with Appendix (PKCS#1 v1.5)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
*/
declare function pkcs1_ssa_1_5(key: RSAPublicKey | RSAPrivateKey, hash?: Hash): {
sign: (M: Uint8Array) => U8;
verify: (M: Uint8Array, S: Uint8Array) => boolean;
};
interface SM2DI {

@@ -1760,2 +1424,385 @@ /**

export { B32, B64, B64URL, type BlockCipher, type BlockCipherInfo, CSV, type Cipher, type Codec, type Digest, type ECDSASignature, type ECIESCiphertext, type ECKeyPair, type ECPrivateKey, type ECPublicKey, FpECC, type FpECCrypto, type FpECPoint, type FpMECParams, type FpSM2Crypto, type FpWECParams, HEX, type Hash, type HashDescription, ISO7816_PAD, type IVBlockCipher, type IVCipher, type IVCipherInfo, type IVStreamCipher, type KDF, type KeyDigest, type KeyHash, type KeyHashDescription, type MGF, NO_PAD, PKCS7_PAD, type RSAPrivateKey, type RSAPublicKey, type RandomPrimeGenerator, type SM2DSASignature, type StreamCipher, type StreamCipherInfo, type TupleDigest, type TupleHash, type TupleHashDescription, U8, UTF8, type X25519, type X25519KeyPair, type X25519PrivateKey, type X25519PublicKey, type X448, type X448KeyPair, type X448PrivateKey, type X448PublicKey, X923_PAD, type XXTEAConfig, ZERO_PAD, type ZUCParams, aes, arc4, arc5, aria, blowfish, bp192r1, bp224r1, bp256r1, bp320r1, bp384r1, bp512r1, camellia, cbc, cfb, createCipher, createHash, createTupleHash, cshake128, cshake256, ctr, curve25519, curve448, des, ecb, eea3, eia3, gcm, genPrime, hkdf, hmac, isProbablePrime, joinBuffer, keccak_p_1600, kmac128, kmac128XOF, kmac256, kmac256XOF, kt128, kt256, md5, mgf1, ofb, p192, p224, p256, p384, p521, parallelhash128, parallelhash128XOF, parallelhash256, parallelhash256XOF, pbkdf2, pcbc, pkcs1_es_1_5, pkcs1_es_oaep, pkcs1_ssa_1_5, pkcs1_ssa_pss, prime192v1, prime256v1, rabbit, rsa, salsa20, secp192k1, secp192r1, secp224k1, secp224r1, secp256k1, secp256r1, secp384r1, secp521r1, sha1, sha224, sha256, sha384, sha3_224, sha3_256, sha3_384, sha3_512, sha512, sha512t, shake128, shake256, sm2, sm2p256v1, sm3, sm4, sponge_1600, t_des, tea, totp, tuplehash128, tuplehash128XOF, tuplehash256, tuplehash256XOF, turboshake128, turboshake256, twofish, w25519, w448, x25519, x448, x963kdf, xtea, xxtea, zuc };
/**
* ARC4 流密码 / stream cipher
*/
declare const arc4: StreamCipher;
/**
* Rabbit 流密码 / stream cipher
*/
declare const rabbit: IVStreamCipher;
/**
* Salsa20 流密码 / Stream Cipher
*/
declare const salsa20: IVStreamCipher;
/**
* 3GPP ZUC 算法用于生成密钥流,每次调用返回一个 32 位的密钥流.
*
* 3GPP ZUC algorithm is used to generate a key stream, each call returns a 32-bit key stream.
*
* ```ts
* const K = new Uint8Array(16)
* const iv = new Uint8Array(16)
* const prg = zuc(K, iv)
* prg() // 32-bit number
* ```
*/
declare function zuc(K: Uint8Array, iv: Uint8Array): () => number;
interface ZUCParams {
/**
* 32-bit counter
*
* if `counter` is `number` type, convert to `Uint8Array` type in little-endian.
*
* 如果 `counter` 为 `number` 类型,则转换为小端存储的 `Uint8Array` 类型.
*/
COUNTER: Uint8Array | number;
/**
* 5-bit bearer
*/
BEARER: number;
/**
* 1-bit direction
*/
DIRECTION: 0 | 1;
/**
* 128-bit key
*/
KEY: Uint8Array;
/**
* 32-bit length
*/
LENGTH: number;
M: Uint8Array;
}
interface ZUC3GPP {
(param: ZUCParams): U8;
}
/**
* 3GPP ZUC 加密算法 / Encryption algorithm
*/
declare const eea3: ZUC3GPP;
/**
* 3GPP ZUC 完整性算法 / Integrity algorithm
*/
declare const eia3: ZUC3GPP;
/**
* FIPS.198-1: 散列消息认证码 (HMAC).
* 如果 `d_size` 大于散列算法的摘要大小, 则回退到散列算法的摘要大小.
*
* FIPS.198-1: The Keyed-Hash Message Authentication Code (HMAC).
* If `d_size` is larger than the hash algorithm's digest size, fallback to the hash algorithm's digest size.
*
* @param {Hash} hash - 散列算法 / hash algorithm
* @param {number} [d_size] - 摘要大小 (bit) / digest size (bit)
* @param {number} [k_size] - 推荐密钥大小 (bit) / recommended key size (bit)
*/
declare function hmac(hash: Hash, d_size?: number, k_size?: number): KeyHash;
/**
* KangarooTwelve 128
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [C] - 自定义参数 / Customization
*/
declare function kt128(d: number, C?: Uint8Array<ArrayBuffer>): Hash;
/**
* KangarooTwelve 256
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [C] - 自定义参数 / Customization
*/
declare function kt256(d: number, C?: Uint8Array<ArrayBuffer>): Hash;
/**
* `Keccak-p` 置换函数 / Permutate Function
*/
interface Keccak_p {
/**
* @param {Uint8Array} S - 状态 / State
*/
(S: Uint8Array): Uint8Array;
}
/**
* `Keccak-p[1600, nr]` 置换函数 / Permutate Function
*
* @param {number} [nr] - 轮数 / Rounds (default: 24)
*/
declare function keccak_p_1600(nr?: number): Keccak_p;
/**
* `SPONGE` 填充函数 / Padding Function
*/
interface SpongePadding {
/**
* @param {Uint8Array} M - 消息 / Message
*/
(M: Uint8Array): Uint8Array;
}
/**
* `SPONGE` & `Keccak-p[1600]`
*
* @param {number} r_byte - 处理速率 / Rate
* @param {number} d_byte - 输出长度 / Digest Size
* @param {SpongePadding} pad - 填充函数 / Padding Function
* @param {Keccak_p} f - Keccak-p 置换函数 / Permutate Function
*/
declare function sponge_1600(r_byte: number, d_byte: number, pad: SpongePadding, f?: Keccak_p): (M: Uint8Array) => U8;
declare const md5: Hash;
declare const sha1: Hash;
declare const sha3_224: Hash;
declare const sha3_256: Hash;
declare const sha3_384: Hash;
declare const sha3_512: Hash;
/**
* @param {number} d - 输出长度 / Digest Size (bit)
*/
declare function shake128(d: number): Hash;
/**
* @param {number} d - 输出长度 / Digest Size (bit)
*/
declare function shake256(d: number): Hash;
/**
* `cSHAKE128` 是 `SHAKE128` 的可定制变体
*
* `cSHAKE128` is a customizable variant of `SHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [N] - 函数名 / Function name
* @param {Uint8Array} [S] - 自定义参数 / Customization
*/
declare function cshake128(d: number, N?: Uint8Array<ArrayBuffer>, S?: Uint8Array<ArrayBuffer>): Hash;
/**
* `cSHAKE256` 是 `SHAKE256` 的可定制变体
*
* `cSHAKE256` is a customizable variant of `SHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [N] - 函数名 / Function name
* @param {Uint8Array} [S] - 自定义参数 / Customization
*/
declare function cshake256(d: number, N?: Uint8Array<ArrayBuffer>, S?: Uint8Array<ArrayBuffer>): Hash;
/**
* Keccak 消息认证码 (KMAC) 算法
* `KMAC128` 是 `KMAC` 的变体, 由 `cSHAKE128` 构建
*
* The Keccak Message Authentication Code (KMAC) algorithm
* `KMAC128` is a variant of `KMAC`, build from `cSHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac128(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* Keccak 消息认证码 (KMAC) 算法
* `KMAC256` 是 `KMAC` 的变体, 由 `cSHAKE256` 构建
*
* The Keccak Message Authentication Code (KMAC) algorithm
* `KMAC256` is a variant of `KMAC`, build from `cSHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac256(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* 可变长度输出的 `KMAC`
* `KMAC128XOF` 是 `KMAC128` 的 XOF 模式, 由 `cSHAKE128` 构建
*
* `KMAC` with Arbitrary-Length Output
* `KMAC128XOF` is a XOF mode of `KMAC128`, build from `cSHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac128XOF(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* 可变长度输出的 `KMAC`
* `KMAC256XOF` 是 `KMAC256` 的 XOF 模式, 由 `cSHAKE256` 构建
*
* `KMAC` with Arbitrary-Length Output
* `KMAC256XOF` is a XOF mode of `KMAC256`, build from `cSHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / recommended key size (bit)
*/
declare function kmac256XOF(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* `TupleHash` 是一个具有可变长度输出的 `SHA3` 派生散列函数, 旨在以一种明确的方式简单地散列输入字符串的元组, 这些字符串中的任何一个或全部都可以是空字符串.
*
* `TupleHash` is a `SHA3` derived hash function with variable-length output that is designed to simply hash a tuple of input strings, any or all of which may be empty strings, in an unambiguous way.
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash128(d: number, S?: Uint8Array): TupleHash;
/**
* `TupleHash` 是一个具有可变长度输出的 `SHA3` 派生散列函数, 旨在以一种明确的方式简单地散列输入字符串的元组, 这些字符串中的任何一个或全部都可以是空字符串.
*
* `TupleHash` is a `SHA3` derived hash function with variable-length output that is designed to simply hash a tuple of input strings, any or all of which may be empty strings, in an unambiguous way.
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash256(d: number, S?: Uint8Array): TupleHash;
/**
* 可变长度输出的 `TupleHash`
*
* `TupleHash` with Arbitrary-Length Output
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash128XOF(d: number, S?: Uint8Array): TupleHash;
/**
* 可变长度输出的 `TupleHash`
*
* `TupleHash` with Arbitrary-Length Output
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash256XOF(d: number, S?: Uint8Array): TupleHash;
/**
* `ParallelHash` 的目的是利用现代处理器中可用的并行性, 支持对非常长的字符串进行高效散列.
*
* The purpose of `ParallelHash` is to support the efficient hashing of very long strings, by taking advantage of the parallelism available in modern processors.
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash128(b: number, d: number, S?: Uint8Array): Hash;
/**
* `ParallelHash` 的目的是利用现代处理器中可用的并行性, 支持对非常长的字符串进行高效散列.
*
* The purpose of `ParallelHash` is to support the efficient hashing of very long strings, by taking advantage of the parallelism available in modern processors.
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash256(b: number, d: number, S?: Uint8Array): Hash;
/**
* 可变长度输出的 `ParallelHash`
*
* `ParallelHash` with Arbitrary-Length Output
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash128XOF(b: number, d: number, S?: Uint8Array): Hash;
/**
* 可变长度输出的 `ParallelHash`
*
* `ParallelHash` with Arbitrary-Length Output
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash256XOF(b: number, d: number, S?: Uint8Array): Hash;
declare const sha224: Hash;
declare const sha256: Hash;
declare const sha384: Hash;
declare const sha512: Hash;
/**
* @param {number} t - 截断长度 / truncation length (bit)
*/
declare function sha512t(t: number): Hash;
declare const sm3: Hash;
interface TOTP {
/**
* 生成 TOTP (时间同步的一次性密码)
*
* Generate TOTP (Time-based One-Time Password)
*
* @param {Uint8Array} secret - 密钥 / Secret key
* @returns {string} - 返回的 TOTP 字符串 / TOTP string
*/
(secret: Uint8Array): string;
}
interface TOTPParams {
/**
* 带密钥的加密散列算法 / Keyed Hashing Algorithm (default: HMAC-SHA1)
*/
mac?: KeyHash;
/**
* 当前时间戳 / Current timestamp (default: Date.now() milliseconds)
*
* 指定此参数时,将不再从 `Date.now()` 获取当前时间戳.
*
* When this parameter is specified, the current timestamp will not be obtained from `Date.now()`.
*/
current?: number;
/**
* 纪元时间戳 / Epoch timestamp (default: 0 milliseconds)
*/
epoch?: number;
/**
* 时间步长 / Time step (default: 30000 milliseconds)
*/
step?: number;
/**
* 计数器 / Counter
*
* `counter = (cuttent_time - epoch_time) / step`
*
* 指定此参数时,将不再从当前时间戳计算计数器.
*
* When this parameter is specified, the counter will not be calculated from the current timestamp.
*/
counter?: number | bigint | Uint8Array;
/**
* 返回的数字位数 / Number of digits in the returned OTP (default: 6)
*/
digits?: number;
}
/**
* 生成 TOTP (时间同步的一次性密码)
*
* Generate TOTP (Time-based One-Time Password)
*
* @param {Uint8Array} secret - 密钥 / Secret key
* @returns {string} - 返回的 TOTP 字符串 / TOTP string
*/
declare function totp(secret: Uint8Array): string;
/**
* 创建 TOTP 函数 / Create a TOTP function
*
* @param {TOTPParams} params - TOTP 参数 / TOTP parameters
* @returns {TOTP} - 返回的 TOTP 函数 / TOTP function
*/
declare function totp(params: TOTPParams): TOTP;
/**
* TurboSHAKE128
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {number} [D] - 域分隔符 / Domain Separator (range: 0x01 ~ 0x7F, default: 0x1F)
*/
declare function turboshake128(d: number, D?: number): Hash;
/**
* TurboSHAKE256
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {number} [D] - 域分隔符 / Domain Separator (range: 0x01 ~ 0x7F, default: 0x1F)
*/
declare function turboshake256(d: number, D?: number): Hash;
export { B32, B64, B64URL, CSV, FpECC, HEX, ISO7816_PAD, NO_PAD, PKCS7_PAD, U8, UTF8, X923_PAD, ZERO_PAD, aes, arc4, arc5, aria, blowfish, bp192r1, bp224r1, bp256r1, bp320r1, bp384r1, bp512r1, camellia, cbc, cfb, createCipher, createHash, createTupleHash, cshake128, cshake256, ctr, curve25519, curve448, des, ecb, eea3, eia3, gcm, genPrime, hkdf, hmac, isProbablePrime, joinBuffer, keccak_p_1600, kmac128, kmac128XOF, kmac256, kmac256XOF, kt128, kt256, md5, mgf1, ofb, p192, p224, p256, p384, p521, parallelhash128, parallelhash128XOF, parallelhash256, parallelhash256XOF, pbkdf2, pcbc, pkcs1_es_1_5, pkcs1_es_oaep, pkcs1_ssa_1_5, pkcs1_ssa_pss, prime192v1, prime256v1, rabbit, rsa, salsa20, scrypt, secp192k1, secp192r1, secp224k1, secp224r1, secp256k1, secp256r1, secp384r1, secp521r1, sha1, sha224, sha256, sha384, sha3_224, sha3_256, sha3_384, sha3_512, sha512, sha512t, shake128, shake256, sm2, sm2p256v1, sm3, sm4, sponge_1600, t_des, tea, totp, tuplehash128, tuplehash128XOF, tuplehash256, tuplehash256XOF, turboshake128, turboshake256, twofish, w25519, w448, x25519, x448, x963kdf, xtea, xxtea, zuc };
export type { BlockCipher, BlockCipherInfo, Cipher, Codec, Digest, ECDSASignature, ECIESCiphertext, ECKeyPair, ECPrivateKey, ECPublicKey, FpECCrypto, FpECPoint, FpMECParams, FpSM2Crypto, FpWECParams, Hash, HashDescription, IVBlockCipher, IVCipher, IVCipherInfo, IVStreamCipher, KDF, KeyDigest, KeyHash, KeyHashDescription, MGF, RSAPrivateKey, RSAPublicKey, RandomPrimeGenerator, SM2DSASignature, StreamCipher, StreamCipherInfo, TupleDigest, TupleHash, TupleHashDescription, X25519, X25519KeyPair, X25519PrivateKey, X25519PublicKey, X448, X448KeyPair, X448PrivateKey, X448PublicKey, XXTEAConfig, ZUCParams };

@@ -0,1 +1,65 @@

/**
* 高级加密标准 (AES) 分组密码算法
*
* Advanced Encryption Standard (AES) block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function aes(b: 128 | 192 | 256): BlockCipher;
/**
* ARC5 分组加密算法 / block cipher algorithm
*
* ```ts
* const spec8 = arc5(8, 8) // ARC5-8/8
* const spec16 = arc5(16, 12) // ARC5-16/12
* const spec32 = arc5(32, 16) // ARC5-32/16 (default)
* const spec64 = arc5(64, 20) // ARC5-64/20
* const spec128 = arc5(128, 24) // ARC5-128/24
* ```
*
* @param {16 | 32 | 64} WORD_SIZE - 工作字长 / Word size (default: 32 bit)
* @param {number} round - 轮数 / Rounds (default: 16)
*/
declare function arc5(WORD_SIZE?: 8 | 16 | 32 | 64 | 128, round?: number): BlockCipher;
/**
* ARIA 分组密码算法 / block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function aria(b: 128 | 192 | 256): BlockCipher;
/**
* Blowfish 分组密码算法 / block cipher algorithm
*/
declare const blowfish: BlockCipher;
/**
* Camellia 分组密码算法 / block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function camellia(b: 128 | 192 | 256): BlockCipher;
/**
* Data Encryption Standard (DES) block cipher algorithm
*
* 数据加密标准(DES)分组密码算法
*/
declare const des: BlockCipher;
/**
* Triple Data Encryption Standard (3DES) block cipher algorithm
*
* 三重数据加密标准(3DES)分组密码算法
*
* @param {128 | 192} l - 密钥长度 / Key Size (bit)
*/
declare function t_des(l: 128 | 192): BlockCipher;
/**
* SM4 分组密码算法 / block cipher algorithm
*/
declare const sm4: BlockCipher;
/** 字符编解码器 / String Codec */

@@ -122,410 +186,2 @@ interface Codec {

/** 随机素数生成器 / Random Prime Generator */
interface RandomPrimeGenerator {
/**
* @param {bigint} b - 位数 / Bits
*/
(b: number): bigint;
}
/** 随机素数生成器 / Random Prime Generator */
declare const genPrime: RandomPrimeGenerator;
/**
* 素性测试: 确定性 >= 1-.5^t
*
* Primality test: deterministic >= 1-.5^t
*
* @param {bigint} n - 待测试的数 / Number to be tested
* @param {number} t - 测试轮数 / Number of tests
*/
declare function isProbablePrime(n: bigint, t?: number): boolean;
interface Digest {
/**
* @param {Uint8Array} M - 消息 / message
*/
(M: Uint8Array): U8;
}
interface HashDescription {
/** 算法名称 / Algorithm name */
ALGORITHM: string;
/** 分块大小 / Block size (byte) */
BLOCK_SIZE: number;
/** 摘要大小 / Digest size (byte) */
DIGEST_SIZE: number;
OID?: string;
}
interface Hash extends Digest, HashDescription {
}
/**
* 散列算法包装器,
* 提供散列算法描述, 以实现 `HMAC` 等拓展算法.
*
* Hash algorithm wrapper,
* provide hash algorithm description to implement extended algorithms such as `HMAC`.
*
* @param {Digest} digest - 摘要函数 / digest function
* @param {HashDescription} description - 算法描述 / algorithm description
*
* ```ts
* const digest: Digest = (M: Uint8Array): U8 => { ... }
* const description: HashDescription = { ... }
* const hash = createHash(digest, description)
* ```
*/
declare const createHash: (digest: Digest, description: HashDescription) => Hash;
interface TupleDigest {
/**
* @param {Uint8Array[]} M - 消息 / message
*/
(M: Uint8Array[]): U8;
}
interface TupleHashDescription extends HashDescription {
}
interface TupleHash extends TupleDigest, TupleHashDescription {
}
/**
* 元组散列算法包装器
*
* Tuple hash algorithm wrapper
*
* @param {TupleDigest} digest - 元组摘要函数 / tuple digest function
* @param {TupleHashDescription} description - 算法描述 / algorithm description
*
* ```ts
* const digest: TupleDigest = (M: Uint8Array[]): U8 => { ... }
* const description: TupleHashDescription = { ... }
* const hash = createTupleHash(digest, description)
* ```
*/
declare const createTupleHash: (digest: TupleDigest, description: TupleHashDescription) => TupleHash;
interface KeyDigest {
/**
* @param {Uint8Array} K - 密钥 / key
* @param {Uint8Array} M - 消息 / message
*/
(K: Uint8Array, M: Uint8Array): U8;
}
interface KeyHashDescription extends HashDescription {
/** 推荐的密钥大小 / Recommended key size (byte) */
KEY_SIZE: number;
}
/** 密钥散列函数 / Keyed hash function */
interface KeyHash extends KeyDigest, KeyHashDescription {
}
declare const md5: Hash;
declare const sha1: Hash;
declare const sha224: Hash;
declare const sha256: Hash;
declare const sha384: Hash;
declare const sha512: Hash;
/**
* @param {number} t - 截断长度 / truncation length (bit)
*/
declare function sha512t(t: number): Hash;
/**
* `Keccak-p` 置换函数 / Permutate Function
*/
interface Keccak_p {
/**
* @param {Uint8Array} S - 状态 / State
*/
(S: Uint8Array): Uint8Array;
}
/**
* `Keccak-p[1600, nr]` 置换函数 / Permutate Function
*
* @param {number} [nr] - 轮数 / Rounds (default: 24)
*/
declare function keccak_p_1600(nr?: number): Keccak_p;
/**
* `SPONGE` 填充函数 / Padding Function
*/
interface SpongePadding {
/**
* @param {Uint8Array} M - 消息 / Message
*/
(M: Uint8Array): Uint8Array;
}
/**
* `SPONGE` & `Keccak-p[1600]`
*
* @param {number} r_byte - 处理速率 / Rate
* @param {number} d_byte - 输出长度 / Digest Size
* @param {SpongePadding} pad - 填充函数 / Padding Function
* @param {Keccak_p} f - Keccak-p 置换函数 / Permutate Function
*/
declare function sponge_1600(r_byte: number, d_byte: number, pad: SpongePadding, f?: Keccak_p): (M: Uint8Array) => U8;
declare const sha3_224: Hash;
declare const sha3_256: Hash;
declare const sha3_384: Hash;
declare const sha3_512: Hash;
/**
* @param {number} d - 输出长度 / Digest Size (bit)
*/
declare function shake128(d: number): Hash;
/**
* @param {number} d - 输出长度 / Digest Size (bit)
*/
declare function shake256(d: number): Hash;
/**
* `cSHAKE128` 是 `SHAKE128` 的可定制变体
*
* `cSHAKE128` is a customizable variant of `SHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [N] - 函数名 / Function name
* @param {Uint8Array} [S] - 自定义参数 / Customization
*/
declare function cshake128(d: number, N?: Uint8Array<ArrayBuffer>, S?: Uint8Array<ArrayBuffer>): Hash;
/**
* `cSHAKE256` 是 `SHAKE256` 的可定制变体
*
* `cSHAKE256` is a customizable variant of `SHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [N] - 函数名 / Function name
* @param {Uint8Array} [S] - 自定义参数 / Customization
*/
declare function cshake256(d: number, N?: Uint8Array<ArrayBuffer>, S?: Uint8Array<ArrayBuffer>): Hash;
/**
* Keccak 消息认证码 (KMAC) 算法
* `KMAC128` 是 `KMAC` 的变体, 由 `cSHAKE128` 构建
*
* The Keccak Message Authentication Code (KMAC) algorithm
* `KMAC128` is a variant of `KMAC`, build from `cSHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac128(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* Keccak 消息认证码 (KMAC) 算法
* `KMAC256` 是 `KMAC` 的变体, 由 `cSHAKE256` 构建
*
* The Keccak Message Authentication Code (KMAC) algorithm
* `KMAC256` is a variant of `KMAC`, build from `cSHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac256(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* 可变长度输出的 `KMAC`
* `KMAC128XOF` 是 `KMAC128` 的 XOF 模式, 由 `cSHAKE128` 构建
*
* `KMAC` with Arbitrary-Length Output
* `KMAC128XOF` is a XOF mode of `KMAC128`, build from `cSHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac128XOF(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* 可变长度输出的 `KMAC`
* `KMAC256XOF` 是 `KMAC256` 的 XOF 模式, 由 `cSHAKE256` 构建
*
* `KMAC` with Arbitrary-Length Output
* `KMAC256XOF` is a XOF mode of `KMAC256`, build from `cSHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / recommended key size (bit)
*/
declare function kmac256XOF(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* `TupleHash` 是一个具有可变长度输出的 `SHA3` 派生散列函数, 旨在以一种明确的方式简单地散列输入字符串的元组, 这些字符串中的任何一个或全部都可以是空字符串.
*
* `TupleHash` is a `SHA3` derived hash function with variable-length output that is designed to simply hash a tuple of input strings, any or all of which may be empty strings, in an unambiguous way.
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash128(d: number, S?: Uint8Array): TupleHash;
/**
* `TupleHash` 是一个具有可变长度输出的 `SHA3` 派生散列函数, 旨在以一种明确的方式简单地散列输入字符串的元组, 这些字符串中的任何一个或全部都可以是空字符串.
*
* `TupleHash` is a `SHA3` derived hash function with variable-length output that is designed to simply hash a tuple of input strings, any or all of which may be empty strings, in an unambiguous way.
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash256(d: number, S?: Uint8Array): TupleHash;
/**
* 可变长度输出的 `TupleHash`
*
* `TupleHash` with Arbitrary-Length Output
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash128XOF(d: number, S?: Uint8Array): TupleHash;
/**
* 可变长度输出的 `TupleHash`
*
* `TupleHash` with Arbitrary-Length Output
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash256XOF(d: number, S?: Uint8Array): TupleHash;
/**
* `ParallelHash` 的目的是利用现代处理器中可用的并行性, 支持对非常长的字符串进行高效散列.
*
* The purpose of `ParallelHash` is to support the efficient hashing of very long strings, by taking advantage of the parallelism available in modern processors.
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash128(b: number, d: number, S?: Uint8Array): Hash;
/**
* `ParallelHash` 的目的是利用现代处理器中可用的并行性, 支持对非常长的字符串进行高效散列.
*
* The purpose of `ParallelHash` is to support the efficient hashing of very long strings, by taking advantage of the parallelism available in modern processors.
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash256(b: number, d: number, S?: Uint8Array): Hash;
/**
* 可变长度输出的 `ParallelHash`
*
* `ParallelHash` with Arbitrary-Length Output
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash128XOF(b: number, d: number, S?: Uint8Array): Hash;
/**
* 可变长度输出的 `ParallelHash`
*
* `ParallelHash` with Arbitrary-Length Output
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash256XOF(b: number, d: number, S?: Uint8Array): Hash;
/**
* TurboSHAKE128
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {number} [D] - 域分隔符 / Domain Separator (range: 0x01 ~ 0x7F, default: 0x1F)
*/
declare function turboshake128(d: number, D?: number): Hash;
/**
* TurboSHAKE256
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {number} [D] - 域分隔符 / Domain Separator (range: 0x01 ~ 0x7F, default: 0x1F)
*/
declare function turboshake256(d: number, D?: number): Hash;
/**
* KangarooTwelve 128
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [C] - 自定义参数 / Customization
*/
declare function kt128(d: number, C?: Uint8Array<ArrayBuffer>): Hash;
/**
* KangarooTwelve 256
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [C] - 自定义参数 / Customization
*/
declare function kt256(d: number, C?: Uint8Array<ArrayBuffer>): Hash;
declare const sm3: Hash;
/**
* FIPS.198-1: 散列消息认证码 (HMAC).
* 如果 `d_size` 大于散列算法的摘要大小, 则回退到散列算法的摘要大小.
*
* FIPS.198-1: The Keyed-Hash Message Authentication Code (HMAC).
* If `d_size` is larger than the hash algorithm's digest size, fallback to the hash algorithm's digest size.
*
* @param {Hash} hash - 散列算法 / hash algorithm
* @param {number} [d_size] - 摘要大小 (bit) / digest size (bit)
* @param {number} [k_size] - 推荐密钥大小 (bit) / recommended key size (bit)
*/
declare function hmac(hash: Hash, d_size?: number, k_size?: number): KeyHash;
interface TOTP {
/**
* 生成 TOTP (时间同步的一次性密码)
*
* Generate TOTP (Time-based One-Time Password)
*
* @param {Uint8Array} secret - 密钥
* @returns {string} - 返回的 TOTP 字符串
*/
(secret: Uint8Array): string;
}
interface TOTPParams {
/**
* 带密钥的加密散列算法 / Keyed Hashing Algorithm (default: HMAC-SHA1)
*/
mac?: KeyHash;
/**
* 当前时间戳 / Current timestamp (default: Date.now() milliseconds)
*
* 指定此参数时,将不再从 `Date.now()` 获取当前时间戳.
*
* When this parameter is specified, the current timestamp will not be obtained from `Date.now()`.
*/
current?: number;
/**
* 纪元时间戳 / Epoch timestamp (default: 0 milliseconds)
*/
epoch?: number;
/**
* 时间步长 / Time step (default: 30000 milliseconds)
*/
step?: number;
/**
* 计数器 / Counter
*
* `counter = (cuttent_time - epoch_time) / step`
*
* 指定此参数时,将不再从当前时间戳计算计数器.
*
* When this parameter is specified, the counter will not be calculated from the current timestamp.
*/
counter?: number | bigint | Uint8Array;
/**
* 返回的数字位数 / Number of digits in the returned OTP (default: 6)
*/
digits?: number;
}
/**
* 生成 TOTP (时间同步的一次性密码)
*
* Generate TOTP (Time-based One-Time Password)
*
* @param {Uint8Array} secret - 密钥
* @returns {string} - 返回的 TOTP 字符串
*/
declare function totp(secret: Uint8Array): string;
/**
* 创建 TOTP 函数 / Create a TOTP function
*
* @param {TOTPParams} params - TOTP 参数
* @returns {TOTP} - 返回的 TOTP 函数
*/
declare function totp(params: TOTPParams): TOTP;
interface Cipherable {

@@ -736,73 +392,2 @@ /**

/**
* SM4 分组密码算法 / block cipher algorithm
*/
declare const sm4: BlockCipher;
/**
* 高级加密标准 (AES) 分组密码算法
*
* Advanced Encryption Standard (AES) block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function aes(b: 128 | 192 | 256): BlockCipher;
/**
* ARIA 分组密码算法 / block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function aria(b: 128 | 192 | 256): BlockCipher;
/**
* Camellia 分组密码算法 / block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function camellia(b: 128 | 192 | 256): BlockCipher;
/**
* Data Encryption Standard (DES) block cipher algorithm
*
* 数据加密标准(DES)分组密码算法
*/
declare const des: BlockCipher;
/**
* Triple Data Encryption Standard (3DES) block cipher algorithm
*
* 三重数据加密标准(3DES)分组密码算法
*
* @param {128 | 192} l - 密钥长度 / Key Size (bit)
*/
declare function t_des(l: 128 | 192): BlockCipher;
/**
* ARC5 分组加密算法 / block cipher algorithm
*
* ```ts
* const spec8 = arc5(8, 8) // ARC5-8/8
* const spec16 = arc5(16, 12) // ARC5-16/12
* const spec32 = arc5(32, 16) // ARC5-32/16 (default)
* const spec64 = arc5(64, 20) // ARC5-64/20
* const spec128 = arc5(128, 24) // ARC5-128/24
* ```
*
* @param {16 | 32 | 64} WORD_SIZE - 工作字长 / Word size (default: 32 bit)
* @param {number} round - 轮数 / Rounds (default: 16)
*/
declare function arc5(WORD_SIZE?: 8 | 16 | 32 | 64 | 128, round?: number): BlockCipher;
/**
* Blowfish 分组密码算法 / block cipher algorithm
*/
declare const blowfish: BlockCipher;
/**
* Twofish 分组密码算法 / block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function twofish(b: 128 | 192 | 256): BlockCipher;
/**
* 微型加密算法 (TEA) 分组密码算法

@@ -864,6 +449,2 @@ *

* Corrected Block TEA (XXTEA) block cipher algorithm
*
* @param {Padding} [config.padding] - 填充方式 / Padding method (default: PKCS7)
* @param {number} [config.round] - 轮数 / Rounds (default: undefined)
* @param {number} [config.BLOCK_SIZE] - 分组大小 / Block size (default: 16)
*/

@@ -873,224 +454,9 @@ declare function xxtea(config?: XXTEAConfig): BlockCipher;

/**
* 3GPP ZUC 算法用于生成密钥流,每次调用返回一个 32 位的密钥流.
* Twofish 分组密码算法 / block cipher algorithm
*
* 3GPP ZUC algorithm is used to generate a key stream, each call returns a 32-bit key stream.
*
* ```ts
* const K = new Uint8Array(16)
* const iv = new Uint8Array(16)
* const prg = zuc(K, iv)
* prg() // 32-bit number
* ```
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function zuc(K: Uint8Array, iv: Uint8Array): () => number;
interface ZUCParams {
/**
* 32-bit counter
*
* if `counter` is `number` type, convert to `Uint8Array` type in little-endian.
*
* 如果 `counter` 为 `number` 类型,则转换为小端存储的 `Uint8Array` 类型.
*/
COUNTER: Uint8Array | number;
/**
* 5-bit bearer
*/
BEARER: number;
/**
* 1-bit direction
*/
DIRECTION: 0 | 1;
/**
* 128-bit key
*/
KEY: Uint8Array;
/**
* 32-bit length
*/
LENGTH: number;
M: Uint8Array;
}
interface ZUC3GPP {
(param: ZUCParams): U8;
}
/**
* 3GPP ZUC 加密算法 / Encryption algorithm
*/
declare const eea3: ZUC3GPP;
/**
* 3GPP ZUC 完整性算法 / Integrity algorithm
*/
declare const eia3: ZUC3GPP;
declare function twofish(b: 128 | 192 | 256): BlockCipher;
/**
* ARC4 流密码 / stream cipher
*/
declare const arc4: StreamCipher;
/**
* Salsa20 流密码 / Stream Cipher
*/
declare const salsa20: IVStreamCipher;
/**
* Rabbit 流密码 / stream cipher
*/
declare const rabbit: IVStreamCipher;
interface RSAPublicKey {
/** 模数 / Modulus */
n: bigint;
/** 公钥指数 / Public Exponent */
e: bigint;
}
interface RSAPrivateKey extends RSAPublicKey {
/** 模数 / Modulus */
n: bigint;
/** 公钥指数 / Public Exponent */
e: bigint;
/** 私钥指数 / Private Exponent */
d: bigint;
p: bigint;
q: bigint;
dP: bigint;
dQ: bigint;
qInv: bigint;
}
interface RSACipherable {
/**
* 使用 RSA 加密原语加密消息
*
* Encrypt message using RSA encryption primitive
*/
encrypt: (M: Uint8Array) => bigint;
/**
* 使用 RSA 解密原语解密密文
*
* Decrypt ciphertext using RSA decryption primitive
*/
decrypt: (C: Uint8Array) => bigint;
}
interface RSAVerifiable {
/**
* 使用 RSA 签名原语对消息签名
*
* Sign message using RSA signature primitive
*/
sign: (M: Uint8Array) => bigint;
/**
* 使用 RSA 验证原语验证签名
*
* Verify signature using RSA verification primitive
*/
verify: (S: Uint8Array) => bigint;
}
/**
* 根据 RSA 私钥长度生成 RSA 密钥对, 并返回 RSA 加密原语和签名原语
*
* Generate RSA key pair according to RSA private key length, and return RSA encryption primitive and signature primitive
*
* @param {number} b - RSA 私钥长度 / RSA private key length
* @param {RandomPrimeGenerator} rpg - 随机素数生成器 / Random prime generator
*/
declare function rsa(b: number, rpg?: RandomPrimeGenerator): RSACipherable & RSAVerifiable & RSAPrivateKey;
/**
* 根据 RSA 公钥或私钥生成 RSA 加密原语和验证原语
*
* Generate RSA encryption primitive and verification primitive according to RSA public or private key
*
* @param {RSAPrivateKey | RSAPublicKey} key - RSA 公钥或私钥 / RSA public or private key
*/
declare function rsa<T extends RSAPrivateKey | RSAPublicKey>(key: T): RSACipherable & RSAVerifiable & T;
interface MGF {
(mdfSeed: Uint8Array, maskLen: number): Uint8Array;
}
/**
* PKCS#1 v2.2 的 掩码生成函数 MGF1
*
* Mask Generation Function MGF1 of PKCS#1 v2.2
*/
declare function mgf1(hash: Hash): MGF;
/**
* 最优非对称加密填充的 RSA 加密方案 (OAEP)
*
* RSA Encryption Scheme with Optimal Asymmetric Encryption Padding (OAEP)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
* @param {MGF} [mgf] - 掩码生成函数 / Mask generation function (default: MGF1)
* @param {Uint8Array} [label] - 标签 / Label (default: empty)
*/
declare function pkcs1_es_oaep(key: RSAPublicKey | RSAPrivateKey, hash?: Hash, mgf?: MGF, label?: Uint8Array<ArrayBuffer>): {
encrypt: (M: Uint8Array) => U8;
decrypt: (C: Uint8Array) => U8;
};
/**
* RSA 加密方案 (PKCS#1 v1.5)
*
* RSA Encryption Scheme (PKCS#1 v1.5)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
*/
declare function pkcs1_es_1_5(key: RSAPublicKey | RSAPrivateKey): {
encrypt: (M: Uint8Array) => U8;
decrypt: (C: Uint8Array) => U8;
};
/**
* 基于 概率签名方案 的 RSA 附录签名方案 (PSS)
*
* RSA Signature Scheme with Appendix - Probabilistic Signature Scheme (PSS)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
* @param {MGF} [mgf] - 掩码生成函数 / Mask generation function (default: MGF1)
* @param {number} [sLen] - 盐长度 / Salt length (default: hash.DIGEST_SIZE)
*/
declare function pkcs1_ssa_pss(key: RSAPublicKey | RSAPrivateKey, hash?: Hash, mgf?: MGF, sLen?: number): {
sign: (M: Uint8Array) => U8;
verify: (M: Uint8Array, S: Uint8Array) => boolean;
};
/**
* RSA 附录签名方案 (PKCS#1 v1.5)
*
* RSA Signature Scheme with Appendix (PKCS#1 v1.5)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
*/
declare function pkcs1_ssa_1_5(key: RSAPublicKey | RSAPrivateKey, hash?: Hash): {
sign: (M: Uint8Array) => U8;
verify: (M: Uint8Array, S: Uint8Array) => boolean;
};
interface KDF {
/**
* @param {number} k_bit - 期望的密钥长度 / output keying material length
* @param {Uint8Array} ikm - 输入密钥材料 / input keying material
* @param {Uint8Array} info - 附加信息 / optional context and application specific information
*/
(k_bit: number, ikm: Uint8Array, info?: Uint8Array): U8;
}
/**
* ANSI-X9.63 Key Derivation Function
*
* ANSI-X9.63 密钥派生函数
*/
declare function x963kdf(hash: Hash): KDF;
/**
* HMAC-based Key Derivation Function (HKDF), please combine `hmac` and `hash` externally to control the behavior of calling `hmac` inside the function.
*
* 基于 HMAC 的密钥派生函数 (HKDF), 请在外部组合 `hmac` 和 `hash` 函数, 以控制在函数内部调用 `hmac` 时的行为.
*/
declare function hkdf(k_hash: KeyHash, salt?: Uint8Array<ArrayBuffer>): KDF;
/**
* Password-Based Key Derivation Function 2 (PBKDF2), please combine `hmac` and `hash` externally to control the behavior of calling `hmac` inside the function.
* Also, PBKDF2 does not use the `info` parameter, if provided, it will be ignored.
*
* PBKDF2 密码基础密钥派生函数 (PBKDF2), 请在外部组合 `hmac` 和 `hash` 函数, 以控制在函数内部调用 `hmac` 时的行为.
* 同时, PBKDF2 不使用 `info` 参数, 如果提供 `info`, 将被忽略.
*/
declare function pbkdf2(k_hash: KeyHash, salt?: Uint8Array<ArrayBuffer>, iterations?: number): KDF;
/**
* 伪射坐标表示的椭圆曲线的点

@@ -1359,2 +725,155 @@ *

interface Digest {
/**
* @param {Uint8Array} M - 消息 / message
*/
(M: Uint8Array): U8;
}
interface HashDescription {
/** 算法名称 / Algorithm name */
ALGORITHM: string;
/** 分块大小 / Block size (byte) */
BLOCK_SIZE: number;
/** 摘要大小 / Digest size (byte) */
DIGEST_SIZE: number;
OID?: string;
}
interface Hash extends Digest, HashDescription {
}
/**
* 散列算法包装器,
* 提供散列算法描述, 以实现 `HMAC` 等拓展算法.
*
* Hash algorithm wrapper,
* provide hash algorithm description to implement extended algorithms such as `HMAC`.
*
* @param {Digest} digest - 摘要函数 / digest function
* @param {HashDescription} description - 算法描述 / algorithm description
*
* ```ts
* const digest: Digest = (M: Uint8Array): U8 => { ... }
* const description: HashDescription = { ... }
* const hash = createHash(digest, description)
* ```
*/
declare const createHash: (digest: Digest, description: HashDescription) => Hash;
interface TupleDigest {
/**
* @param {Uint8Array[]} M - 消息 / message
*/
(M: Uint8Array[]): U8;
}
interface TupleHashDescription extends HashDescription {
}
interface TupleHash extends TupleDigest, TupleHashDescription {
}
/**
* 元组散列算法包装器
*
* Tuple hash algorithm wrapper
*
* @param {TupleDigest} digest - 元组摘要函数 / tuple digest function
* @param {TupleHashDescription} description - 算法描述 / algorithm description
*
* ```ts
* const digest: TupleDigest = (M: Uint8Array[]): U8 => { ... }
* const description: TupleHashDescription = { ... }
* const hash = createTupleHash(digest, description)
* ```
*/
declare const createTupleHash: (digest: TupleDigest, description: TupleHashDescription) => TupleHash;
interface KeyDigest {
/**
* @param {Uint8Array} K - 密钥 / key
* @param {Uint8Array} M - 消息 / message
*/
(K: Uint8Array, M: Uint8Array): U8;
}
interface KeyHashDescription extends HashDescription {
/** 推荐的密钥大小 / Recommended key size (byte) */
KEY_SIZE: number;
}
/** 密钥散列函数 / Keyed hash function */
interface KeyHash extends KeyDigest, KeyHashDescription {
}
interface KDF {
/**
* @param {number} k_byte - 期望的密钥长度 / output keying material length
* @param {Uint8Array} ikm - 输入密钥材料 / input keying material
* @param {Uint8Array} salt - 盐 / salt value
*/
(k_byte: number, ikm: Uint8Array, salt?: Uint8Array): U8;
}
/**
* ANSI-X9.63 Key Derivation Function
*
* ANSI-X9.63 密钥派生函数
*/
declare function x963kdf(hash: Hash, info?: Uint8Array<ArrayBuffer>): KDF;
/**
* HMAC-based Key Derivation Function (HKDF), please combine `hmac` and `hash` externally to control the behavior of calling `hmac` inside the function.
*
* 基于 HMAC 的密钥派生函数 (HKDF), 请在外部组合 `hmac` 和 `hash` 函数, 以控制在函数内部调用 `hmac` 时的行为.
*/
declare function hkdf(k_hash: KeyHash, info?: Uint8Array<ArrayBuffer>): KDF;
/**
* Password-Based Key Derivation Function 2 (PBKDF2), please combine `hmac` and `hash` externally to control the behavior of calling `hmac` inside the function.
*
* PBKDF2 密码基础密钥派生函数 (PBKDF2), 请在外部组合 `hmac` 和 `hash` 函数, 以控制在函数内部调用 `hmac` 时的行为.
*/
declare function pbkdf2(k_hash: KeyHash, iterations?: number): KDF;
interface ScryptConfig {
/**
* 开销因子 / Cost factor (default: 16384)
*
* 必须是 2 的幂
*
* Must be a power of 2
*/
N?: number;
/**
* 块数 / Block count (default: 8)
*/
r?: number;
/**
* 并行因子 / Parallelization factor (default: 1)
*/
p?: number;
/**
* 最大内存使用量 / Maximum memory usage
*
* 如果设置为 0,则不限制内存使用量
*
* If set to 0, there is no limit on memory usage
*
* (default: 0x40000400 bytes, 1GB + 1KB)
*/
maxmem?: number;
/**
* 密钥派生函数 / Key Derivation Function
*
* scrypt 标准使用了 `PBKDF2-HMAC-SHA256` 作为 KDF。
* 该参数允许用户指定其他 KDF,改变 scrypt 的内部行为。
*
* 注意: 这不是 `scrypt` 的标准用法且缺乏相关的安全分析。
*
* The scrypt standard uses `PBKDF2-HMAC-SHA256` as the KDF.
* This parameter allows users to specify a different KDF, changing the internal behavior of scrypt.
*
* Note: This is not the standard usage of `scrypt` and lacks relevant security analysis.
*
* (default: pbkdf2(hmac(sha256), 1))
*/
kdf?: KDF;
}
/**
* Scrypt Key Derivation Function
*
* Scrypt 密钥派生函数
*
* Based on https://github.com/paulmillr/noble-hashes
*/
declare function scrypt(config?: ScryptConfig): KDF;
interface ECPublicKey<T = bigint | Uint8Array> {

@@ -1549,2 +1068,147 @@ /** 椭圆曲线公钥 / Elliptic Curve Public Key */

/** 随机素数生成器 / Random Prime Generator */
interface RandomPrimeGenerator {
/**
* @param {bigint} b - 位数 / Bits
*/
(b: number): bigint;
}
/** 随机素数生成器 / Random Prime Generator */
declare const genPrime: RandomPrimeGenerator;
/**
* 素性测试: 确定性 >= 1-.5^t
*
* Primality test: deterministic >= 1-.5^t
*
* @param {bigint} n - 待测试的数 / Number to be tested
* @param {number} t - 测试轮数 / Number of tests
*/
declare function isProbablePrime(n: bigint, t?: number): boolean;
interface RSAPublicKey {
/** 模数 / Modulus */
n: bigint;
/** 公钥指数 / Public Exponent */
e: bigint;
}
interface RSAPrivateKey extends RSAPublicKey {
/** 模数 / Modulus */
n: bigint;
/** 公钥指数 / Public Exponent */
e: bigint;
/** 私钥指数 / Private Exponent */
d: bigint;
p: bigint;
q: bigint;
dP: bigint;
dQ: bigint;
qInv: bigint;
}
interface RSACipherable {
/**
* 使用 RSA 加密原语加密消息
*
* Encrypt message using RSA encryption primitive
*/
encrypt: (M: Uint8Array) => bigint;
/**
* 使用 RSA 解密原语解密密文
*
* Decrypt ciphertext using RSA decryption primitive
*/
decrypt: (C: Uint8Array) => bigint;
}
interface RSAVerifiable {
/**
* 使用 RSA 签名原语对消息签名
*
* Sign message using RSA signature primitive
*/
sign: (M: Uint8Array) => bigint;
/**
* 使用 RSA 验证原语验证签名
*
* Verify signature using RSA verification primitive
*/
verify: (S: Uint8Array) => bigint;
}
/**
* 根据 RSA 私钥长度生成 RSA 密钥对, 并返回 RSA 加密原语和签名原语
*
* Generate RSA key pair according to RSA private key length, and return RSA encryption primitive and signature primitive
*
* @param {number} b - RSA 私钥长度 / RSA private key length
* @param {RandomPrimeGenerator} rpg - 随机素数生成器 / Random prime generator
*/
declare function rsa(b: number, rpg?: RandomPrimeGenerator): RSACipherable & RSAVerifiable & RSAPrivateKey;
/**
* 根据 RSA 公钥或私钥生成 RSA 加密原语和验证原语
*
* Generate RSA encryption primitive and verification primitive according to RSA public or private key
*
* @param {RSAPrivateKey | RSAPublicKey} key - RSA 公钥或私钥 / RSA public or private key
*/
declare function rsa<T extends RSAPrivateKey | RSAPublicKey>(key: T): RSACipherable & RSAVerifiable & T;
interface MGF {
(mdfSeed: Uint8Array, maskLen: number): Uint8Array;
}
/**
* PKCS#1 v2.2 的 掩码生成函数 MGF1
*
* Mask Generation Function MGF1 of PKCS#1 v2.2
*/
declare function mgf1(hash: Hash): MGF;
/**
* 最优非对称加密填充的 RSA 加密方案 (OAEP)
*
* RSA Encryption Scheme with Optimal Asymmetric Encryption Padding (OAEP)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
* @param {MGF} [mgf] - 掩码生成函数 / Mask generation function (default: MGF1)
* @param {Uint8Array} [label] - 标签 / Label (default: empty)
*/
declare function pkcs1_es_oaep(key: RSAPublicKey | RSAPrivateKey, hash?: Hash, mgf?: MGF, label?: Uint8Array<ArrayBuffer>): {
encrypt: (M: Uint8Array) => U8;
decrypt: (C: Uint8Array) => U8;
};
/**
* RSA 加密方案 (PKCS#1 v1.5)
*
* RSA Encryption Scheme (PKCS#1 v1.5)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
*/
declare function pkcs1_es_1_5(key: RSAPublicKey | RSAPrivateKey): {
encrypt: (M: Uint8Array) => U8;
decrypt: (C: Uint8Array) => U8;
};
/**
* 基于 概率签名方案 的 RSA 附录签名方案 (PSS)
*
* RSA Signature Scheme with Appendix - Probabilistic Signature Scheme (PSS)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
* @param {MGF} [mgf] - 掩码生成函数 / Mask generation function (default: MGF1)
* @param {number} [sLen] - 盐长度 / Salt length (default: hash.DIGEST_SIZE)
*/
declare function pkcs1_ssa_pss(key: RSAPublicKey | RSAPrivateKey, hash?: Hash, mgf?: MGF, sLen?: number): {
sign: (M: Uint8Array) => U8;
verify: (M: Uint8Array, S: Uint8Array) => boolean;
};
/**
* RSA 附录签名方案 (PKCS#1 v1.5)
*
* RSA Signature Scheme with Appendix (PKCS#1 v1.5)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
*/
declare function pkcs1_ssa_1_5(key: RSAPublicKey | RSAPrivateKey, hash?: Hash): {
sign: (M: Uint8Array) => U8;
verify: (M: Uint8Array, S: Uint8Array) => boolean;
};
interface SM2DI {

@@ -1760,2 +1424,385 @@ /**

export { B32, B64, B64URL, type BlockCipher, type BlockCipherInfo, CSV, type Cipher, type Codec, type Digest, type ECDSASignature, type ECIESCiphertext, type ECKeyPair, type ECPrivateKey, type ECPublicKey, FpECC, type FpECCrypto, type FpECPoint, type FpMECParams, type FpSM2Crypto, type FpWECParams, HEX, type Hash, type HashDescription, ISO7816_PAD, type IVBlockCipher, type IVCipher, type IVCipherInfo, type IVStreamCipher, type KDF, type KeyDigest, type KeyHash, type KeyHashDescription, type MGF, NO_PAD, PKCS7_PAD, type RSAPrivateKey, type RSAPublicKey, type RandomPrimeGenerator, type SM2DSASignature, type StreamCipher, type StreamCipherInfo, type TupleDigest, type TupleHash, type TupleHashDescription, U8, UTF8, type X25519, type X25519KeyPair, type X25519PrivateKey, type X25519PublicKey, type X448, type X448KeyPair, type X448PrivateKey, type X448PublicKey, X923_PAD, type XXTEAConfig, ZERO_PAD, type ZUCParams, aes, arc4, arc5, aria, blowfish, bp192r1, bp224r1, bp256r1, bp320r1, bp384r1, bp512r1, camellia, cbc, cfb, createCipher, createHash, createTupleHash, cshake128, cshake256, ctr, curve25519, curve448, des, ecb, eea3, eia3, gcm, genPrime, hkdf, hmac, isProbablePrime, joinBuffer, keccak_p_1600, kmac128, kmac128XOF, kmac256, kmac256XOF, kt128, kt256, md5, mgf1, ofb, p192, p224, p256, p384, p521, parallelhash128, parallelhash128XOF, parallelhash256, parallelhash256XOF, pbkdf2, pcbc, pkcs1_es_1_5, pkcs1_es_oaep, pkcs1_ssa_1_5, pkcs1_ssa_pss, prime192v1, prime256v1, rabbit, rsa, salsa20, secp192k1, secp192r1, secp224k1, secp224r1, secp256k1, secp256r1, secp384r1, secp521r1, sha1, sha224, sha256, sha384, sha3_224, sha3_256, sha3_384, sha3_512, sha512, sha512t, shake128, shake256, sm2, sm2p256v1, sm3, sm4, sponge_1600, t_des, tea, totp, tuplehash128, tuplehash128XOF, tuplehash256, tuplehash256XOF, turboshake128, turboshake256, twofish, w25519, w448, x25519, x448, x963kdf, xtea, xxtea, zuc };
/**
* ARC4 流密码 / stream cipher
*/
declare const arc4: StreamCipher;
/**
* Rabbit 流密码 / stream cipher
*/
declare const rabbit: IVStreamCipher;
/**
* Salsa20 流密码 / Stream Cipher
*/
declare const salsa20: IVStreamCipher;
/**
* 3GPP ZUC 算法用于生成密钥流,每次调用返回一个 32 位的密钥流.
*
* 3GPP ZUC algorithm is used to generate a key stream, each call returns a 32-bit key stream.
*
* ```ts
* const K = new Uint8Array(16)
* const iv = new Uint8Array(16)
* const prg = zuc(K, iv)
* prg() // 32-bit number
* ```
*/
declare function zuc(K: Uint8Array, iv: Uint8Array): () => number;
interface ZUCParams {
/**
* 32-bit counter
*
* if `counter` is `number` type, convert to `Uint8Array` type in little-endian.
*
* 如果 `counter` 为 `number` 类型,则转换为小端存储的 `Uint8Array` 类型.
*/
COUNTER: Uint8Array | number;
/**
* 5-bit bearer
*/
BEARER: number;
/**
* 1-bit direction
*/
DIRECTION: 0 | 1;
/**
* 128-bit key
*/
KEY: Uint8Array;
/**
* 32-bit length
*/
LENGTH: number;
M: Uint8Array;
}
interface ZUC3GPP {
(param: ZUCParams): U8;
}
/**
* 3GPP ZUC 加密算法 / Encryption algorithm
*/
declare const eea3: ZUC3GPP;
/**
* 3GPP ZUC 完整性算法 / Integrity algorithm
*/
declare const eia3: ZUC3GPP;
/**
* FIPS.198-1: 散列消息认证码 (HMAC).
* 如果 `d_size` 大于散列算法的摘要大小, 则回退到散列算法的摘要大小.
*
* FIPS.198-1: The Keyed-Hash Message Authentication Code (HMAC).
* If `d_size` is larger than the hash algorithm's digest size, fallback to the hash algorithm's digest size.
*
* @param {Hash} hash - 散列算法 / hash algorithm
* @param {number} [d_size] - 摘要大小 (bit) / digest size (bit)
* @param {number} [k_size] - 推荐密钥大小 (bit) / recommended key size (bit)
*/
declare function hmac(hash: Hash, d_size?: number, k_size?: number): KeyHash;
/**
* KangarooTwelve 128
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [C] - 自定义参数 / Customization
*/
declare function kt128(d: number, C?: Uint8Array<ArrayBuffer>): Hash;
/**
* KangarooTwelve 256
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [C] - 自定义参数 / Customization
*/
declare function kt256(d: number, C?: Uint8Array<ArrayBuffer>): Hash;
/**
* `Keccak-p` 置换函数 / Permutate Function
*/
interface Keccak_p {
/**
* @param {Uint8Array} S - 状态 / State
*/
(S: Uint8Array): Uint8Array;
}
/**
* `Keccak-p[1600, nr]` 置换函数 / Permutate Function
*
* @param {number} [nr] - 轮数 / Rounds (default: 24)
*/
declare function keccak_p_1600(nr?: number): Keccak_p;
/**
* `SPONGE` 填充函数 / Padding Function
*/
interface SpongePadding {
/**
* @param {Uint8Array} M - 消息 / Message
*/
(M: Uint8Array): Uint8Array;
}
/**
* `SPONGE` & `Keccak-p[1600]`
*
* @param {number} r_byte - 处理速率 / Rate
* @param {number} d_byte - 输出长度 / Digest Size
* @param {SpongePadding} pad - 填充函数 / Padding Function
* @param {Keccak_p} f - Keccak-p 置换函数 / Permutate Function
*/
declare function sponge_1600(r_byte: number, d_byte: number, pad: SpongePadding, f?: Keccak_p): (M: Uint8Array) => U8;
declare const md5: Hash;
declare const sha1: Hash;
declare const sha3_224: Hash;
declare const sha3_256: Hash;
declare const sha3_384: Hash;
declare const sha3_512: Hash;
/**
* @param {number} d - 输出长度 / Digest Size (bit)
*/
declare function shake128(d: number): Hash;
/**
* @param {number} d - 输出长度 / Digest Size (bit)
*/
declare function shake256(d: number): Hash;
/**
* `cSHAKE128` 是 `SHAKE128` 的可定制变体
*
* `cSHAKE128` is a customizable variant of `SHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [N] - 函数名 / Function name
* @param {Uint8Array} [S] - 自定义参数 / Customization
*/
declare function cshake128(d: number, N?: Uint8Array<ArrayBuffer>, S?: Uint8Array<ArrayBuffer>): Hash;
/**
* `cSHAKE256` 是 `SHAKE256` 的可定制变体
*
* `cSHAKE256` is a customizable variant of `SHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [N] - 函数名 / Function name
* @param {Uint8Array} [S] - 自定义参数 / Customization
*/
declare function cshake256(d: number, N?: Uint8Array<ArrayBuffer>, S?: Uint8Array<ArrayBuffer>): Hash;
/**
* Keccak 消息认证码 (KMAC) 算法
* `KMAC128` 是 `KMAC` 的变体, 由 `cSHAKE128` 构建
*
* The Keccak Message Authentication Code (KMAC) algorithm
* `KMAC128` is a variant of `KMAC`, build from `cSHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac128(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* Keccak 消息认证码 (KMAC) 算法
* `KMAC256` 是 `KMAC` 的变体, 由 `cSHAKE256` 构建
*
* The Keccak Message Authentication Code (KMAC) algorithm
* `KMAC256` is a variant of `KMAC`, build from `cSHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac256(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* 可变长度输出的 `KMAC`
* `KMAC128XOF` 是 `KMAC128` 的 XOF 模式, 由 `cSHAKE128` 构建
*
* `KMAC` with Arbitrary-Length Output
* `KMAC128XOF` is a XOF mode of `KMAC128`, build from `cSHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac128XOF(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* 可变长度输出的 `KMAC`
* `KMAC256XOF` 是 `KMAC256` 的 XOF 模式, 由 `cSHAKE256` 构建
*
* `KMAC` with Arbitrary-Length Output
* `KMAC256XOF` is a XOF mode of `KMAC256`, build from `cSHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / recommended key size (bit)
*/
declare function kmac256XOF(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* `TupleHash` 是一个具有可变长度输出的 `SHA3` 派生散列函数, 旨在以一种明确的方式简单地散列输入字符串的元组, 这些字符串中的任何一个或全部都可以是空字符串.
*
* `TupleHash` is a `SHA3` derived hash function with variable-length output that is designed to simply hash a tuple of input strings, any or all of which may be empty strings, in an unambiguous way.
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash128(d: number, S?: Uint8Array): TupleHash;
/**
* `TupleHash` 是一个具有可变长度输出的 `SHA3` 派生散列函数, 旨在以一种明确的方式简单地散列输入字符串的元组, 这些字符串中的任何一个或全部都可以是空字符串.
*
* `TupleHash` is a `SHA3` derived hash function with variable-length output that is designed to simply hash a tuple of input strings, any or all of which may be empty strings, in an unambiguous way.
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash256(d: number, S?: Uint8Array): TupleHash;
/**
* 可变长度输出的 `TupleHash`
*
* `TupleHash` with Arbitrary-Length Output
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash128XOF(d: number, S?: Uint8Array): TupleHash;
/**
* 可变长度输出的 `TupleHash`
*
* `TupleHash` with Arbitrary-Length Output
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash256XOF(d: number, S?: Uint8Array): TupleHash;
/**
* `ParallelHash` 的目的是利用现代处理器中可用的并行性, 支持对非常长的字符串进行高效散列.
*
* The purpose of `ParallelHash` is to support the efficient hashing of very long strings, by taking advantage of the parallelism available in modern processors.
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash128(b: number, d: number, S?: Uint8Array): Hash;
/**
* `ParallelHash` 的目的是利用现代处理器中可用的并行性, 支持对非常长的字符串进行高效散列.
*
* The purpose of `ParallelHash` is to support the efficient hashing of very long strings, by taking advantage of the parallelism available in modern processors.
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash256(b: number, d: number, S?: Uint8Array): Hash;
/**
* 可变长度输出的 `ParallelHash`
*
* `ParallelHash` with Arbitrary-Length Output
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash128XOF(b: number, d: number, S?: Uint8Array): Hash;
/**
* 可变长度输出的 `ParallelHash`
*
* `ParallelHash` with Arbitrary-Length Output
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash256XOF(b: number, d: number, S?: Uint8Array): Hash;
declare const sha224: Hash;
declare const sha256: Hash;
declare const sha384: Hash;
declare const sha512: Hash;
/**
* @param {number} t - 截断长度 / truncation length (bit)
*/
declare function sha512t(t: number): Hash;
declare const sm3: Hash;
interface TOTP {
/**
* 生成 TOTP (时间同步的一次性密码)
*
* Generate TOTP (Time-based One-Time Password)
*
* @param {Uint8Array} secret - 密钥 / Secret key
* @returns {string} - 返回的 TOTP 字符串 / TOTP string
*/
(secret: Uint8Array): string;
}
interface TOTPParams {
/**
* 带密钥的加密散列算法 / Keyed Hashing Algorithm (default: HMAC-SHA1)
*/
mac?: KeyHash;
/**
* 当前时间戳 / Current timestamp (default: Date.now() milliseconds)
*
* 指定此参数时,将不再从 `Date.now()` 获取当前时间戳.
*
* When this parameter is specified, the current timestamp will not be obtained from `Date.now()`.
*/
current?: number;
/**
* 纪元时间戳 / Epoch timestamp (default: 0 milliseconds)
*/
epoch?: number;
/**
* 时间步长 / Time step (default: 30000 milliseconds)
*/
step?: number;
/**
* 计数器 / Counter
*
* `counter = (cuttent_time - epoch_time) / step`
*
* 指定此参数时,将不再从当前时间戳计算计数器.
*
* When this parameter is specified, the counter will not be calculated from the current timestamp.
*/
counter?: number | bigint | Uint8Array;
/**
* 返回的数字位数 / Number of digits in the returned OTP (default: 6)
*/
digits?: number;
}
/**
* 生成 TOTP (时间同步的一次性密码)
*
* Generate TOTP (Time-based One-Time Password)
*
* @param {Uint8Array} secret - 密钥 / Secret key
* @returns {string} - 返回的 TOTP 字符串 / TOTP string
*/
declare function totp(secret: Uint8Array): string;
/**
* 创建 TOTP 函数 / Create a TOTP function
*
* @param {TOTPParams} params - TOTP 参数 / TOTP parameters
* @returns {TOTP} - 返回的 TOTP 函数 / TOTP function
*/
declare function totp(params: TOTPParams): TOTP;
/**
* TurboSHAKE128
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {number} [D] - 域分隔符 / Domain Separator (range: 0x01 ~ 0x7F, default: 0x1F)
*/
declare function turboshake128(d: number, D?: number): Hash;
/**
* TurboSHAKE256
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {number} [D] - 域分隔符 / Domain Separator (range: 0x01 ~ 0x7F, default: 0x1F)
*/
declare function turboshake256(d: number, D?: number): Hash;
export { B32, B64, B64URL, CSV, FpECC, HEX, ISO7816_PAD, NO_PAD, PKCS7_PAD, U8, UTF8, X923_PAD, ZERO_PAD, aes, arc4, arc5, aria, blowfish, bp192r1, bp224r1, bp256r1, bp320r1, bp384r1, bp512r1, camellia, cbc, cfb, createCipher, createHash, createTupleHash, cshake128, cshake256, ctr, curve25519, curve448, des, ecb, eea3, eia3, gcm, genPrime, hkdf, hmac, isProbablePrime, joinBuffer, keccak_p_1600, kmac128, kmac128XOF, kmac256, kmac256XOF, kt128, kt256, md5, mgf1, ofb, p192, p224, p256, p384, p521, parallelhash128, parallelhash128XOF, parallelhash256, parallelhash256XOF, pbkdf2, pcbc, pkcs1_es_1_5, pkcs1_es_oaep, pkcs1_ssa_1_5, pkcs1_ssa_pss, prime192v1, prime256v1, rabbit, rsa, salsa20, scrypt, secp192k1, secp192r1, secp224k1, secp224r1, secp256k1, secp256r1, secp384r1, secp521r1, sha1, sha224, sha256, sha384, sha3_224, sha3_256, sha3_384, sha3_512, sha512, sha512t, shake128, shake256, sm2, sm2p256v1, sm3, sm4, sponge_1600, t_des, tea, totp, tuplehash128, tuplehash128XOF, tuplehash256, tuplehash256XOF, turboshake128, turboshake256, twofish, w25519, w448, x25519, x448, x963kdf, xtea, xxtea, zuc };
export type { BlockCipher, BlockCipherInfo, Cipher, Codec, Digest, ECDSASignature, ECIESCiphertext, ECKeyPair, ECPrivateKey, ECPublicKey, FpECCrypto, FpECPoint, FpMECParams, FpSM2Crypto, FpWECParams, Hash, HashDescription, IVBlockCipher, IVCipher, IVCipherInfo, IVStreamCipher, KDF, KeyDigest, KeyHash, KeyHashDescription, MGF, RSAPrivateKey, RSAPublicKey, RandomPrimeGenerator, SM2DSASignature, StreamCipher, StreamCipherInfo, TupleDigest, TupleHash, TupleHashDescription, X25519, X25519KeyPair, X25519PrivateKey, X25519PublicKey, X448, X448KeyPair, X448PrivateKey, X448PublicKey, XXTEAConfig, ZUCParams };

@@ -0,1 +1,65 @@

/**
* 高级加密标准 (AES) 分组密码算法
*
* Advanced Encryption Standard (AES) block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function aes(b: 128 | 192 | 256): BlockCipher;
/**
* ARC5 分组加密算法 / block cipher algorithm
*
* ```ts
* const spec8 = arc5(8, 8) // ARC5-8/8
* const spec16 = arc5(16, 12) // ARC5-16/12
* const spec32 = arc5(32, 16) // ARC5-32/16 (default)
* const spec64 = arc5(64, 20) // ARC5-64/20
* const spec128 = arc5(128, 24) // ARC5-128/24
* ```
*
* @param {16 | 32 | 64} WORD_SIZE - 工作字长 / Word size (default: 32 bit)
* @param {number} round - 轮数 / Rounds (default: 16)
*/
declare function arc5(WORD_SIZE?: 8 | 16 | 32 | 64 | 128, round?: number): BlockCipher;
/**
* ARIA 分组密码算法 / block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function aria(b: 128 | 192 | 256): BlockCipher;
/**
* Blowfish 分组密码算法 / block cipher algorithm
*/
declare const blowfish: BlockCipher;
/**
* Camellia 分组密码算法 / block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function camellia(b: 128 | 192 | 256): BlockCipher;
/**
* Data Encryption Standard (DES) block cipher algorithm
*
* 数据加密标准(DES)分组密码算法
*/
declare const des: BlockCipher;
/**
* Triple Data Encryption Standard (3DES) block cipher algorithm
*
* 三重数据加密标准(3DES)分组密码算法
*
* @param {128 | 192} l - 密钥长度 / Key Size (bit)
*/
declare function t_des(l: 128 | 192): BlockCipher;
/**
* SM4 分组密码算法 / block cipher algorithm
*/
declare const sm4: BlockCipher;
/** 字符编解码器 / String Codec */

@@ -122,410 +186,2 @@ interface Codec {

/** 随机素数生成器 / Random Prime Generator */
interface RandomPrimeGenerator {
/**
* @param {bigint} b - 位数 / Bits
*/
(b: number): bigint;
}
/** 随机素数生成器 / Random Prime Generator */
declare const genPrime: RandomPrimeGenerator;
/**
* 素性测试: 确定性 >= 1-.5^t
*
* Primality test: deterministic >= 1-.5^t
*
* @param {bigint} n - 待测试的数 / Number to be tested
* @param {number} t - 测试轮数 / Number of tests
*/
declare function isProbablePrime(n: bigint, t?: number): boolean;
interface Digest {
/**
* @param {Uint8Array} M - 消息 / message
*/
(M: Uint8Array): U8;
}
interface HashDescription {
/** 算法名称 / Algorithm name */
ALGORITHM: string;
/** 分块大小 / Block size (byte) */
BLOCK_SIZE: number;
/** 摘要大小 / Digest size (byte) */
DIGEST_SIZE: number;
OID?: string;
}
interface Hash extends Digest, HashDescription {
}
/**
* 散列算法包装器,
* 提供散列算法描述, 以实现 `HMAC` 等拓展算法.
*
* Hash algorithm wrapper,
* provide hash algorithm description to implement extended algorithms such as `HMAC`.
*
* @param {Digest} digest - 摘要函数 / digest function
* @param {HashDescription} description - 算法描述 / algorithm description
*
* ```ts
* const digest: Digest = (M: Uint8Array): U8 => { ... }
* const description: HashDescription = { ... }
* const hash = createHash(digest, description)
* ```
*/
declare const createHash: (digest: Digest, description: HashDescription) => Hash;
interface TupleDigest {
/**
* @param {Uint8Array[]} M - 消息 / message
*/
(M: Uint8Array[]): U8;
}
interface TupleHashDescription extends HashDescription {
}
interface TupleHash extends TupleDigest, TupleHashDescription {
}
/**
* 元组散列算法包装器
*
* Tuple hash algorithm wrapper
*
* @param {TupleDigest} digest - 元组摘要函数 / tuple digest function
* @param {TupleHashDescription} description - 算法描述 / algorithm description
*
* ```ts
* const digest: TupleDigest = (M: Uint8Array[]): U8 => { ... }
* const description: TupleHashDescription = { ... }
* const hash = createTupleHash(digest, description)
* ```
*/
declare const createTupleHash: (digest: TupleDigest, description: TupleHashDescription) => TupleHash;
interface KeyDigest {
/**
* @param {Uint8Array} K - 密钥 / key
* @param {Uint8Array} M - 消息 / message
*/
(K: Uint8Array, M: Uint8Array): U8;
}
interface KeyHashDescription extends HashDescription {
/** 推荐的密钥大小 / Recommended key size (byte) */
KEY_SIZE: number;
}
/** 密钥散列函数 / Keyed hash function */
interface KeyHash extends KeyDigest, KeyHashDescription {
}
declare const md5: Hash;
declare const sha1: Hash;
declare const sha224: Hash;
declare const sha256: Hash;
declare const sha384: Hash;
declare const sha512: Hash;
/**
* @param {number} t - 截断长度 / truncation length (bit)
*/
declare function sha512t(t: number): Hash;
/**
* `Keccak-p` 置换函数 / Permutate Function
*/
interface Keccak_p {
/**
* @param {Uint8Array} S - 状态 / State
*/
(S: Uint8Array): Uint8Array;
}
/**
* `Keccak-p[1600, nr]` 置换函数 / Permutate Function
*
* @param {number} [nr] - 轮数 / Rounds (default: 24)
*/
declare function keccak_p_1600(nr?: number): Keccak_p;
/**
* `SPONGE` 填充函数 / Padding Function
*/
interface SpongePadding {
/**
* @param {Uint8Array} M - 消息 / Message
*/
(M: Uint8Array): Uint8Array;
}
/**
* `SPONGE` & `Keccak-p[1600]`
*
* @param {number} r_byte - 处理速率 / Rate
* @param {number} d_byte - 输出长度 / Digest Size
* @param {SpongePadding} pad - 填充函数 / Padding Function
* @param {Keccak_p} f - Keccak-p 置换函数 / Permutate Function
*/
declare function sponge_1600(r_byte: number, d_byte: number, pad: SpongePadding, f?: Keccak_p): (M: Uint8Array) => U8;
declare const sha3_224: Hash;
declare const sha3_256: Hash;
declare const sha3_384: Hash;
declare const sha3_512: Hash;
/**
* @param {number} d - 输出长度 / Digest Size (bit)
*/
declare function shake128(d: number): Hash;
/**
* @param {number} d - 输出长度 / Digest Size (bit)
*/
declare function shake256(d: number): Hash;
/**
* `cSHAKE128` 是 `SHAKE128` 的可定制变体
*
* `cSHAKE128` is a customizable variant of `SHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [N] - 函数名 / Function name
* @param {Uint8Array} [S] - 自定义参数 / Customization
*/
declare function cshake128(d: number, N?: Uint8Array<ArrayBuffer>, S?: Uint8Array<ArrayBuffer>): Hash;
/**
* `cSHAKE256` 是 `SHAKE256` 的可定制变体
*
* `cSHAKE256` is a customizable variant of `SHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [N] - 函数名 / Function name
* @param {Uint8Array} [S] - 自定义参数 / Customization
*/
declare function cshake256(d: number, N?: Uint8Array<ArrayBuffer>, S?: Uint8Array<ArrayBuffer>): Hash;
/**
* Keccak 消息认证码 (KMAC) 算法
* `KMAC128` 是 `KMAC` 的变体, 由 `cSHAKE128` 构建
*
* The Keccak Message Authentication Code (KMAC) algorithm
* `KMAC128` is a variant of `KMAC`, build from `cSHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac128(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* Keccak 消息认证码 (KMAC) 算法
* `KMAC256` 是 `KMAC` 的变体, 由 `cSHAKE256` 构建
*
* The Keccak Message Authentication Code (KMAC) algorithm
* `KMAC256` is a variant of `KMAC`, build from `cSHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac256(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* 可变长度输出的 `KMAC`
* `KMAC128XOF` 是 `KMAC128` 的 XOF 模式, 由 `cSHAKE128` 构建
*
* `KMAC` with Arbitrary-Length Output
* `KMAC128XOF` is a XOF mode of `KMAC128`, build from `cSHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac128XOF(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* 可变长度输出的 `KMAC`
* `KMAC256XOF` 是 `KMAC256` 的 XOF 模式, 由 `cSHAKE256` 构建
*
* `KMAC` with Arbitrary-Length Output
* `KMAC256XOF` is a XOF mode of `KMAC256`, build from `cSHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / recommended key size (bit)
*/
declare function kmac256XOF(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* `TupleHash` 是一个具有可变长度输出的 `SHA3` 派生散列函数, 旨在以一种明确的方式简单地散列输入字符串的元组, 这些字符串中的任何一个或全部都可以是空字符串.
*
* `TupleHash` is a `SHA3` derived hash function with variable-length output that is designed to simply hash a tuple of input strings, any or all of which may be empty strings, in an unambiguous way.
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash128(d: number, S?: Uint8Array): TupleHash;
/**
* `TupleHash` 是一个具有可变长度输出的 `SHA3` 派生散列函数, 旨在以一种明确的方式简单地散列输入字符串的元组, 这些字符串中的任何一个或全部都可以是空字符串.
*
* `TupleHash` is a `SHA3` derived hash function with variable-length output that is designed to simply hash a tuple of input strings, any or all of which may be empty strings, in an unambiguous way.
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash256(d: number, S?: Uint8Array): TupleHash;
/**
* 可变长度输出的 `TupleHash`
*
* `TupleHash` with Arbitrary-Length Output
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash128XOF(d: number, S?: Uint8Array): TupleHash;
/**
* 可变长度输出的 `TupleHash`
*
* `TupleHash` with Arbitrary-Length Output
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash256XOF(d: number, S?: Uint8Array): TupleHash;
/**
* `ParallelHash` 的目的是利用现代处理器中可用的并行性, 支持对非常长的字符串进行高效散列.
*
* The purpose of `ParallelHash` is to support the efficient hashing of very long strings, by taking advantage of the parallelism available in modern processors.
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash128(b: number, d: number, S?: Uint8Array): Hash;
/**
* `ParallelHash` 的目的是利用现代处理器中可用的并行性, 支持对非常长的字符串进行高效散列.
*
* The purpose of `ParallelHash` is to support the efficient hashing of very long strings, by taking advantage of the parallelism available in modern processors.
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash256(b: number, d: number, S?: Uint8Array): Hash;
/**
* 可变长度输出的 `ParallelHash`
*
* `ParallelHash` with Arbitrary-Length Output
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash128XOF(b: number, d: number, S?: Uint8Array): Hash;
/**
* 可变长度输出的 `ParallelHash`
*
* `ParallelHash` with Arbitrary-Length Output
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash256XOF(b: number, d: number, S?: Uint8Array): Hash;
/**
* TurboSHAKE128
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {number} [D] - 域分隔符 / Domain Separator (range: 0x01 ~ 0x7F, default: 0x1F)
*/
declare function turboshake128(d: number, D?: number): Hash;
/**
* TurboSHAKE256
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {number} [D] - 域分隔符 / Domain Separator (range: 0x01 ~ 0x7F, default: 0x1F)
*/
declare function turboshake256(d: number, D?: number): Hash;
/**
* KangarooTwelve 128
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [C] - 自定义参数 / Customization
*/
declare function kt128(d: number, C?: Uint8Array<ArrayBuffer>): Hash;
/**
* KangarooTwelve 256
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [C] - 自定义参数 / Customization
*/
declare function kt256(d: number, C?: Uint8Array<ArrayBuffer>): Hash;
declare const sm3: Hash;
/**
* FIPS.198-1: 散列消息认证码 (HMAC).
* 如果 `d_size` 大于散列算法的摘要大小, 则回退到散列算法的摘要大小.
*
* FIPS.198-1: The Keyed-Hash Message Authentication Code (HMAC).
* If `d_size` is larger than the hash algorithm's digest size, fallback to the hash algorithm's digest size.
*
* @param {Hash} hash - 散列算法 / hash algorithm
* @param {number} [d_size] - 摘要大小 (bit) / digest size (bit)
* @param {number} [k_size] - 推荐密钥大小 (bit) / recommended key size (bit)
*/
declare function hmac(hash: Hash, d_size?: number, k_size?: number): KeyHash;
interface TOTP {
/**
* 生成 TOTP (时间同步的一次性密码)
*
* Generate TOTP (Time-based One-Time Password)
*
* @param {Uint8Array} secret - 密钥
* @returns {string} - 返回的 TOTP 字符串
*/
(secret: Uint8Array): string;
}
interface TOTPParams {
/**
* 带密钥的加密散列算法 / Keyed Hashing Algorithm (default: HMAC-SHA1)
*/
mac?: KeyHash;
/**
* 当前时间戳 / Current timestamp (default: Date.now() milliseconds)
*
* 指定此参数时,将不再从 `Date.now()` 获取当前时间戳.
*
* When this parameter is specified, the current timestamp will not be obtained from `Date.now()`.
*/
current?: number;
/**
* 纪元时间戳 / Epoch timestamp (default: 0 milliseconds)
*/
epoch?: number;
/**
* 时间步长 / Time step (default: 30000 milliseconds)
*/
step?: number;
/**
* 计数器 / Counter
*
* `counter = (cuttent_time - epoch_time) / step`
*
* 指定此参数时,将不再从当前时间戳计算计数器.
*
* When this parameter is specified, the counter will not be calculated from the current timestamp.
*/
counter?: number | bigint | Uint8Array;
/**
* 返回的数字位数 / Number of digits in the returned OTP (default: 6)
*/
digits?: number;
}
/**
* 生成 TOTP (时间同步的一次性密码)
*
* Generate TOTP (Time-based One-Time Password)
*
* @param {Uint8Array} secret - 密钥
* @returns {string} - 返回的 TOTP 字符串
*/
declare function totp(secret: Uint8Array): string;
/**
* 创建 TOTP 函数 / Create a TOTP function
*
* @param {TOTPParams} params - TOTP 参数
* @returns {TOTP} - 返回的 TOTP 函数
*/
declare function totp(params: TOTPParams): TOTP;
interface Cipherable {

@@ -736,73 +392,2 @@ /**

/**
* SM4 分组密码算法 / block cipher algorithm
*/
declare const sm4: BlockCipher;
/**
* 高级加密标准 (AES) 分组密码算法
*
* Advanced Encryption Standard (AES) block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function aes(b: 128 | 192 | 256): BlockCipher;
/**
* ARIA 分组密码算法 / block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function aria(b: 128 | 192 | 256): BlockCipher;
/**
* Camellia 分组密码算法 / block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function camellia(b: 128 | 192 | 256): BlockCipher;
/**
* Data Encryption Standard (DES) block cipher algorithm
*
* 数据加密标准(DES)分组密码算法
*/
declare const des: BlockCipher;
/**
* Triple Data Encryption Standard (3DES) block cipher algorithm
*
* 三重数据加密标准(3DES)分组密码算法
*
* @param {128 | 192} l - 密钥长度 / Key Size (bit)
*/
declare function t_des(l: 128 | 192): BlockCipher;
/**
* ARC5 分组加密算法 / block cipher algorithm
*
* ```ts
* const spec8 = arc5(8, 8) // ARC5-8/8
* const spec16 = arc5(16, 12) // ARC5-16/12
* const spec32 = arc5(32, 16) // ARC5-32/16 (default)
* const spec64 = arc5(64, 20) // ARC5-64/20
* const spec128 = arc5(128, 24) // ARC5-128/24
* ```
*
* @param {16 | 32 | 64} WORD_SIZE - 工作字长 / Word size (default: 32 bit)
* @param {number} round - 轮数 / Rounds (default: 16)
*/
declare function arc5(WORD_SIZE?: 8 | 16 | 32 | 64 | 128, round?: number): BlockCipher;
/**
* Blowfish 分组密码算法 / block cipher algorithm
*/
declare const blowfish: BlockCipher;
/**
* Twofish 分组密码算法 / block cipher algorithm
*
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function twofish(b: 128 | 192 | 256): BlockCipher;
/**
* 微型加密算法 (TEA) 分组密码算法

@@ -864,6 +449,2 @@ *

* Corrected Block TEA (XXTEA) block cipher algorithm
*
* @param {Padding} [config.padding] - 填充方式 / Padding method (default: PKCS7)
* @param {number} [config.round] - 轮数 / Rounds (default: undefined)
* @param {number} [config.BLOCK_SIZE] - 分组大小 / Block size (default: 16)
*/

@@ -873,224 +454,9 @@ declare function xxtea(config?: XXTEAConfig): BlockCipher;

/**
* 3GPP ZUC 算法用于生成密钥流,每次调用返回一个 32 位的密钥流.
* Twofish 分组密码算法 / block cipher algorithm
*
* 3GPP ZUC algorithm is used to generate a key stream, each call returns a 32-bit key stream.
*
* ```ts
* const K = new Uint8Array(16)
* const iv = new Uint8Array(16)
* const prg = zuc(K, iv)
* prg() // 32-bit number
* ```
* @param {128 | 192 | 256} b - 密钥长度 / Key size (bit)
*/
declare function zuc(K: Uint8Array, iv: Uint8Array): () => number;
interface ZUCParams {
/**
* 32-bit counter
*
* if `counter` is `number` type, convert to `Uint8Array` type in little-endian.
*
* 如果 `counter` 为 `number` 类型,则转换为小端存储的 `Uint8Array` 类型.
*/
COUNTER: Uint8Array | number;
/**
* 5-bit bearer
*/
BEARER: number;
/**
* 1-bit direction
*/
DIRECTION: 0 | 1;
/**
* 128-bit key
*/
KEY: Uint8Array;
/**
* 32-bit length
*/
LENGTH: number;
M: Uint8Array;
}
interface ZUC3GPP {
(param: ZUCParams): U8;
}
/**
* 3GPP ZUC 加密算法 / Encryption algorithm
*/
declare const eea3: ZUC3GPP;
/**
* 3GPP ZUC 完整性算法 / Integrity algorithm
*/
declare const eia3: ZUC3GPP;
declare function twofish(b: 128 | 192 | 256): BlockCipher;
/**
* ARC4 流密码 / stream cipher
*/
declare const arc4: StreamCipher;
/**
* Salsa20 流密码 / Stream Cipher
*/
declare const salsa20: IVStreamCipher;
/**
* Rabbit 流密码 / stream cipher
*/
declare const rabbit: IVStreamCipher;
interface RSAPublicKey {
/** 模数 / Modulus */
n: bigint;
/** 公钥指数 / Public Exponent */
e: bigint;
}
interface RSAPrivateKey extends RSAPublicKey {
/** 模数 / Modulus */
n: bigint;
/** 公钥指数 / Public Exponent */
e: bigint;
/** 私钥指数 / Private Exponent */
d: bigint;
p: bigint;
q: bigint;
dP: bigint;
dQ: bigint;
qInv: bigint;
}
interface RSACipherable {
/**
* 使用 RSA 加密原语加密消息
*
* Encrypt message using RSA encryption primitive
*/
encrypt: (M: Uint8Array) => bigint;
/**
* 使用 RSA 解密原语解密密文
*
* Decrypt ciphertext using RSA decryption primitive
*/
decrypt: (C: Uint8Array) => bigint;
}
interface RSAVerifiable {
/**
* 使用 RSA 签名原语对消息签名
*
* Sign message using RSA signature primitive
*/
sign: (M: Uint8Array) => bigint;
/**
* 使用 RSA 验证原语验证签名
*
* Verify signature using RSA verification primitive
*/
verify: (S: Uint8Array) => bigint;
}
/**
* 根据 RSA 私钥长度生成 RSA 密钥对, 并返回 RSA 加密原语和签名原语
*
* Generate RSA key pair according to RSA private key length, and return RSA encryption primitive and signature primitive
*
* @param {number} b - RSA 私钥长度 / RSA private key length
* @param {RandomPrimeGenerator} rpg - 随机素数生成器 / Random prime generator
*/
declare function rsa(b: number, rpg?: RandomPrimeGenerator): RSACipherable & RSAVerifiable & RSAPrivateKey;
/**
* 根据 RSA 公钥或私钥生成 RSA 加密原语和验证原语
*
* Generate RSA encryption primitive and verification primitive according to RSA public or private key
*
* @param {RSAPrivateKey | RSAPublicKey} key - RSA 公钥或私钥 / RSA public or private key
*/
declare function rsa<T extends RSAPrivateKey | RSAPublicKey>(key: T): RSACipherable & RSAVerifiable & T;
interface MGF {
(mdfSeed: Uint8Array, maskLen: number): Uint8Array;
}
/**
* PKCS#1 v2.2 的 掩码生成函数 MGF1
*
* Mask Generation Function MGF1 of PKCS#1 v2.2
*/
declare function mgf1(hash: Hash): MGF;
/**
* 最优非对称加密填充的 RSA 加密方案 (OAEP)
*
* RSA Encryption Scheme with Optimal Asymmetric Encryption Padding (OAEP)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
* @param {MGF} [mgf] - 掩码生成函数 / Mask generation function (default: MGF1)
* @param {Uint8Array} [label] - 标签 / Label (default: empty)
*/
declare function pkcs1_es_oaep(key: RSAPublicKey | RSAPrivateKey, hash?: Hash, mgf?: MGF, label?: Uint8Array<ArrayBuffer>): {
encrypt: (M: Uint8Array) => U8;
decrypt: (C: Uint8Array) => U8;
};
/**
* RSA 加密方案 (PKCS#1 v1.5)
*
* RSA Encryption Scheme (PKCS#1 v1.5)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
*/
declare function pkcs1_es_1_5(key: RSAPublicKey | RSAPrivateKey): {
encrypt: (M: Uint8Array) => U8;
decrypt: (C: Uint8Array) => U8;
};
/**
* 基于 概率签名方案 的 RSA 附录签名方案 (PSS)
*
* RSA Signature Scheme with Appendix - Probabilistic Signature Scheme (PSS)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
* @param {MGF} [mgf] - 掩码生成函数 / Mask generation function (default: MGF1)
* @param {number} [sLen] - 盐长度 / Salt length (default: hash.DIGEST_SIZE)
*/
declare function pkcs1_ssa_pss(key: RSAPublicKey | RSAPrivateKey, hash?: Hash, mgf?: MGF, sLen?: number): {
sign: (M: Uint8Array) => U8;
verify: (M: Uint8Array, S: Uint8Array) => boolean;
};
/**
* RSA 附录签名方案 (PKCS#1 v1.5)
*
* RSA Signature Scheme with Appendix (PKCS#1 v1.5)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
*/
declare function pkcs1_ssa_1_5(key: RSAPublicKey | RSAPrivateKey, hash?: Hash): {
sign: (M: Uint8Array) => U8;
verify: (M: Uint8Array, S: Uint8Array) => boolean;
};
interface KDF {
/**
* @param {number} k_bit - 期望的密钥长度 / output keying material length
* @param {Uint8Array} ikm - 输入密钥材料 / input keying material
* @param {Uint8Array} info - 附加信息 / optional context and application specific information
*/
(k_bit: number, ikm: Uint8Array, info?: Uint8Array): U8;
}
/**
* ANSI-X9.63 Key Derivation Function
*
* ANSI-X9.63 密钥派生函数
*/
declare function x963kdf(hash: Hash): KDF;
/**
* HMAC-based Key Derivation Function (HKDF), please combine `hmac` and `hash` externally to control the behavior of calling `hmac` inside the function.
*
* 基于 HMAC 的密钥派生函数 (HKDF), 请在外部组合 `hmac` 和 `hash` 函数, 以控制在函数内部调用 `hmac` 时的行为.
*/
declare function hkdf(k_hash: KeyHash, salt?: Uint8Array<ArrayBuffer>): KDF;
/**
* Password-Based Key Derivation Function 2 (PBKDF2), please combine `hmac` and `hash` externally to control the behavior of calling `hmac` inside the function.
* Also, PBKDF2 does not use the `info` parameter, if provided, it will be ignored.
*
* PBKDF2 密码基础密钥派生函数 (PBKDF2), 请在外部组合 `hmac` 和 `hash` 函数, 以控制在函数内部调用 `hmac` 时的行为.
* 同时, PBKDF2 不使用 `info` 参数, 如果提供 `info`, 将被忽略.
*/
declare function pbkdf2(k_hash: KeyHash, salt?: Uint8Array<ArrayBuffer>, iterations?: number): KDF;
/**
* 伪射坐标表示的椭圆曲线的点

@@ -1359,2 +725,155 @@ *

interface Digest {
/**
* @param {Uint8Array} M - 消息 / message
*/
(M: Uint8Array): U8;
}
interface HashDescription {
/** 算法名称 / Algorithm name */
ALGORITHM: string;
/** 分块大小 / Block size (byte) */
BLOCK_SIZE: number;
/** 摘要大小 / Digest size (byte) */
DIGEST_SIZE: number;
OID?: string;
}
interface Hash extends Digest, HashDescription {
}
/**
* 散列算法包装器,
* 提供散列算法描述, 以实现 `HMAC` 等拓展算法.
*
* Hash algorithm wrapper,
* provide hash algorithm description to implement extended algorithms such as `HMAC`.
*
* @param {Digest} digest - 摘要函数 / digest function
* @param {HashDescription} description - 算法描述 / algorithm description
*
* ```ts
* const digest: Digest = (M: Uint8Array): U8 => { ... }
* const description: HashDescription = { ... }
* const hash = createHash(digest, description)
* ```
*/
declare const createHash: (digest: Digest, description: HashDescription) => Hash;
interface TupleDigest {
/**
* @param {Uint8Array[]} M - 消息 / message
*/
(M: Uint8Array[]): U8;
}
interface TupleHashDescription extends HashDescription {
}
interface TupleHash extends TupleDigest, TupleHashDescription {
}
/**
* 元组散列算法包装器
*
* Tuple hash algorithm wrapper
*
* @param {TupleDigest} digest - 元组摘要函数 / tuple digest function
* @param {TupleHashDescription} description - 算法描述 / algorithm description
*
* ```ts
* const digest: TupleDigest = (M: Uint8Array[]): U8 => { ... }
* const description: TupleHashDescription = { ... }
* const hash = createTupleHash(digest, description)
* ```
*/
declare const createTupleHash: (digest: TupleDigest, description: TupleHashDescription) => TupleHash;
interface KeyDigest {
/**
* @param {Uint8Array} K - 密钥 / key
* @param {Uint8Array} M - 消息 / message
*/
(K: Uint8Array, M: Uint8Array): U8;
}
interface KeyHashDescription extends HashDescription {
/** 推荐的密钥大小 / Recommended key size (byte) */
KEY_SIZE: number;
}
/** 密钥散列函数 / Keyed hash function */
interface KeyHash extends KeyDigest, KeyHashDescription {
}
interface KDF {
/**
* @param {number} k_byte - 期望的密钥长度 / output keying material length
* @param {Uint8Array} ikm - 输入密钥材料 / input keying material
* @param {Uint8Array} salt - 盐 / salt value
*/
(k_byte: number, ikm: Uint8Array, salt?: Uint8Array): U8;
}
/**
* ANSI-X9.63 Key Derivation Function
*
* ANSI-X9.63 密钥派生函数
*/
declare function x963kdf(hash: Hash, info?: Uint8Array<ArrayBuffer>): KDF;
/**
* HMAC-based Key Derivation Function (HKDF), please combine `hmac` and `hash` externally to control the behavior of calling `hmac` inside the function.
*
* 基于 HMAC 的密钥派生函数 (HKDF), 请在外部组合 `hmac` 和 `hash` 函数, 以控制在函数内部调用 `hmac` 时的行为.
*/
declare function hkdf(k_hash: KeyHash, info?: Uint8Array<ArrayBuffer>): KDF;
/**
* Password-Based Key Derivation Function 2 (PBKDF2), please combine `hmac` and `hash` externally to control the behavior of calling `hmac` inside the function.
*
* PBKDF2 密码基础密钥派生函数 (PBKDF2), 请在外部组合 `hmac` 和 `hash` 函数, 以控制在函数内部调用 `hmac` 时的行为.
*/
declare function pbkdf2(k_hash: KeyHash, iterations?: number): KDF;
interface ScryptConfig {
/**
* 开销因子 / Cost factor (default: 16384)
*
* 必须是 2 的幂
*
* Must be a power of 2
*/
N?: number;
/**
* 块数 / Block count (default: 8)
*/
r?: number;
/**
* 并行因子 / Parallelization factor (default: 1)
*/
p?: number;
/**
* 最大内存使用量 / Maximum memory usage
*
* 如果设置为 0,则不限制内存使用量
*
* If set to 0, there is no limit on memory usage
*
* (default: 0x40000400 bytes, 1GB + 1KB)
*/
maxmem?: number;
/**
* 密钥派生函数 / Key Derivation Function
*
* scrypt 标准使用了 `PBKDF2-HMAC-SHA256` 作为 KDF。
* 该参数允许用户指定其他 KDF,改变 scrypt 的内部行为。
*
* 注意: 这不是 `scrypt` 的标准用法且缺乏相关的安全分析。
*
* The scrypt standard uses `PBKDF2-HMAC-SHA256` as the KDF.
* This parameter allows users to specify a different KDF, changing the internal behavior of scrypt.
*
* Note: This is not the standard usage of `scrypt` and lacks relevant security analysis.
*
* (default: pbkdf2(hmac(sha256), 1))
*/
kdf?: KDF;
}
/**
* Scrypt Key Derivation Function
*
* Scrypt 密钥派生函数
*
* Based on https://github.com/paulmillr/noble-hashes
*/
declare function scrypt(config?: ScryptConfig): KDF;
interface ECPublicKey<T = bigint | Uint8Array> {

@@ -1549,2 +1068,147 @@ /** 椭圆曲线公钥 / Elliptic Curve Public Key */

/** 随机素数生成器 / Random Prime Generator */
interface RandomPrimeGenerator {
/**
* @param {bigint} b - 位数 / Bits
*/
(b: number): bigint;
}
/** 随机素数生成器 / Random Prime Generator */
declare const genPrime: RandomPrimeGenerator;
/**
* 素性测试: 确定性 >= 1-.5^t
*
* Primality test: deterministic >= 1-.5^t
*
* @param {bigint} n - 待测试的数 / Number to be tested
* @param {number} t - 测试轮数 / Number of tests
*/
declare function isProbablePrime(n: bigint, t?: number): boolean;
interface RSAPublicKey {
/** 模数 / Modulus */
n: bigint;
/** 公钥指数 / Public Exponent */
e: bigint;
}
interface RSAPrivateKey extends RSAPublicKey {
/** 模数 / Modulus */
n: bigint;
/** 公钥指数 / Public Exponent */
e: bigint;
/** 私钥指数 / Private Exponent */
d: bigint;
p: bigint;
q: bigint;
dP: bigint;
dQ: bigint;
qInv: bigint;
}
interface RSACipherable {
/**
* 使用 RSA 加密原语加密消息
*
* Encrypt message using RSA encryption primitive
*/
encrypt: (M: Uint8Array) => bigint;
/**
* 使用 RSA 解密原语解密密文
*
* Decrypt ciphertext using RSA decryption primitive
*/
decrypt: (C: Uint8Array) => bigint;
}
interface RSAVerifiable {
/**
* 使用 RSA 签名原语对消息签名
*
* Sign message using RSA signature primitive
*/
sign: (M: Uint8Array) => bigint;
/**
* 使用 RSA 验证原语验证签名
*
* Verify signature using RSA verification primitive
*/
verify: (S: Uint8Array) => bigint;
}
/**
* 根据 RSA 私钥长度生成 RSA 密钥对, 并返回 RSA 加密原语和签名原语
*
* Generate RSA key pair according to RSA private key length, and return RSA encryption primitive and signature primitive
*
* @param {number} b - RSA 私钥长度 / RSA private key length
* @param {RandomPrimeGenerator} rpg - 随机素数生成器 / Random prime generator
*/
declare function rsa(b: number, rpg?: RandomPrimeGenerator): RSACipherable & RSAVerifiable & RSAPrivateKey;
/**
* 根据 RSA 公钥或私钥生成 RSA 加密原语和验证原语
*
* Generate RSA encryption primitive and verification primitive according to RSA public or private key
*
* @param {RSAPrivateKey | RSAPublicKey} key - RSA 公钥或私钥 / RSA public or private key
*/
declare function rsa<T extends RSAPrivateKey | RSAPublicKey>(key: T): RSACipherable & RSAVerifiable & T;
interface MGF {
(mdfSeed: Uint8Array, maskLen: number): Uint8Array;
}
/**
* PKCS#1 v2.2 的 掩码生成函数 MGF1
*
* Mask Generation Function MGF1 of PKCS#1 v2.2
*/
declare function mgf1(hash: Hash): MGF;
/**
* 最优非对称加密填充的 RSA 加密方案 (OAEP)
*
* RSA Encryption Scheme with Optimal Asymmetric Encryption Padding (OAEP)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
* @param {MGF} [mgf] - 掩码生成函数 / Mask generation function (default: MGF1)
* @param {Uint8Array} [label] - 标签 / Label (default: empty)
*/
declare function pkcs1_es_oaep(key: RSAPublicKey | RSAPrivateKey, hash?: Hash, mgf?: MGF, label?: Uint8Array<ArrayBuffer>): {
encrypt: (M: Uint8Array) => U8;
decrypt: (C: Uint8Array) => U8;
};
/**
* RSA 加密方案 (PKCS#1 v1.5)
*
* RSA Encryption Scheme (PKCS#1 v1.5)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
*/
declare function pkcs1_es_1_5(key: RSAPublicKey | RSAPrivateKey): {
encrypt: (M: Uint8Array) => U8;
decrypt: (C: Uint8Array) => U8;
};
/**
* 基于 概率签名方案 的 RSA 附录签名方案 (PSS)
*
* RSA Signature Scheme with Appendix - Probabilistic Signature Scheme (PSS)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
* @param {MGF} [mgf] - 掩码生成函数 / Mask generation function (default: MGF1)
* @param {number} [sLen] - 盐长度 / Salt length (default: hash.DIGEST_SIZE)
*/
declare function pkcs1_ssa_pss(key: RSAPublicKey | RSAPrivateKey, hash?: Hash, mgf?: MGF, sLen?: number): {
sign: (M: Uint8Array) => U8;
verify: (M: Uint8Array, S: Uint8Array) => boolean;
};
/**
* RSA 附录签名方案 (PKCS#1 v1.5)
*
* RSA Signature Scheme with Appendix (PKCS#1 v1.5)
*
* @param {RSAPublicKey | RSAPrivateKey} key - RSA 公钥或私钥 / RSA public or private key
* @param {Hash} [hash] - 散列函数 / Hash function (default: SHA-256)
*/
declare function pkcs1_ssa_1_5(key: RSAPublicKey | RSAPrivateKey, hash?: Hash): {
sign: (M: Uint8Array) => U8;
verify: (M: Uint8Array, S: Uint8Array) => boolean;
};
interface SM2DI {

@@ -1760,2 +1424,385 @@ /**

export { B32, B64, B64URL, type BlockCipher, type BlockCipherInfo, CSV, type Cipher, type Codec, type Digest, type ECDSASignature, type ECIESCiphertext, type ECKeyPair, type ECPrivateKey, type ECPublicKey, FpECC, type FpECCrypto, type FpECPoint, type FpMECParams, type FpSM2Crypto, type FpWECParams, HEX, type Hash, type HashDescription, ISO7816_PAD, type IVBlockCipher, type IVCipher, type IVCipherInfo, type IVStreamCipher, type KDF, type KeyDigest, type KeyHash, type KeyHashDescription, type MGF, NO_PAD, PKCS7_PAD, type RSAPrivateKey, type RSAPublicKey, type RandomPrimeGenerator, type SM2DSASignature, type StreamCipher, type StreamCipherInfo, type TupleDigest, type TupleHash, type TupleHashDescription, U8, UTF8, type X25519, type X25519KeyPair, type X25519PrivateKey, type X25519PublicKey, type X448, type X448KeyPair, type X448PrivateKey, type X448PublicKey, X923_PAD, type XXTEAConfig, ZERO_PAD, type ZUCParams, aes, arc4, arc5, aria, blowfish, bp192r1, bp224r1, bp256r1, bp320r1, bp384r1, bp512r1, camellia, cbc, cfb, createCipher, createHash, createTupleHash, cshake128, cshake256, ctr, curve25519, curve448, des, ecb, eea3, eia3, gcm, genPrime, hkdf, hmac, isProbablePrime, joinBuffer, keccak_p_1600, kmac128, kmac128XOF, kmac256, kmac256XOF, kt128, kt256, md5, mgf1, ofb, p192, p224, p256, p384, p521, parallelhash128, parallelhash128XOF, parallelhash256, parallelhash256XOF, pbkdf2, pcbc, pkcs1_es_1_5, pkcs1_es_oaep, pkcs1_ssa_1_5, pkcs1_ssa_pss, prime192v1, prime256v1, rabbit, rsa, salsa20, secp192k1, secp192r1, secp224k1, secp224r1, secp256k1, secp256r1, secp384r1, secp521r1, sha1, sha224, sha256, sha384, sha3_224, sha3_256, sha3_384, sha3_512, sha512, sha512t, shake128, shake256, sm2, sm2p256v1, sm3, sm4, sponge_1600, t_des, tea, totp, tuplehash128, tuplehash128XOF, tuplehash256, tuplehash256XOF, turboshake128, turboshake256, twofish, w25519, w448, x25519, x448, x963kdf, xtea, xxtea, zuc };
/**
* ARC4 流密码 / stream cipher
*/
declare const arc4: StreamCipher;
/**
* Rabbit 流密码 / stream cipher
*/
declare const rabbit: IVStreamCipher;
/**
* Salsa20 流密码 / Stream Cipher
*/
declare const salsa20: IVStreamCipher;
/**
* 3GPP ZUC 算法用于生成密钥流,每次调用返回一个 32 位的密钥流.
*
* 3GPP ZUC algorithm is used to generate a key stream, each call returns a 32-bit key stream.
*
* ```ts
* const K = new Uint8Array(16)
* const iv = new Uint8Array(16)
* const prg = zuc(K, iv)
* prg() // 32-bit number
* ```
*/
declare function zuc(K: Uint8Array, iv: Uint8Array): () => number;
interface ZUCParams {
/**
* 32-bit counter
*
* if `counter` is `number` type, convert to `Uint8Array` type in little-endian.
*
* 如果 `counter` 为 `number` 类型,则转换为小端存储的 `Uint8Array` 类型.
*/
COUNTER: Uint8Array | number;
/**
* 5-bit bearer
*/
BEARER: number;
/**
* 1-bit direction
*/
DIRECTION: 0 | 1;
/**
* 128-bit key
*/
KEY: Uint8Array;
/**
* 32-bit length
*/
LENGTH: number;
M: Uint8Array;
}
interface ZUC3GPP {
(param: ZUCParams): U8;
}
/**
* 3GPP ZUC 加密算法 / Encryption algorithm
*/
declare const eea3: ZUC3GPP;
/**
* 3GPP ZUC 完整性算法 / Integrity algorithm
*/
declare const eia3: ZUC3GPP;
/**
* FIPS.198-1: 散列消息认证码 (HMAC).
* 如果 `d_size` 大于散列算法的摘要大小, 则回退到散列算法的摘要大小.
*
* FIPS.198-1: The Keyed-Hash Message Authentication Code (HMAC).
* If `d_size` is larger than the hash algorithm's digest size, fallback to the hash algorithm's digest size.
*
* @param {Hash} hash - 散列算法 / hash algorithm
* @param {number} [d_size] - 摘要大小 (bit) / digest size (bit)
* @param {number} [k_size] - 推荐密钥大小 (bit) / recommended key size (bit)
*/
declare function hmac(hash: Hash, d_size?: number, k_size?: number): KeyHash;
/**
* KangarooTwelve 128
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [C] - 自定义参数 / Customization
*/
declare function kt128(d: number, C?: Uint8Array<ArrayBuffer>): Hash;
/**
* KangarooTwelve 256
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [C] - 自定义参数 / Customization
*/
declare function kt256(d: number, C?: Uint8Array<ArrayBuffer>): Hash;
/**
* `Keccak-p` 置换函数 / Permutate Function
*/
interface Keccak_p {
/**
* @param {Uint8Array} S - 状态 / State
*/
(S: Uint8Array): Uint8Array;
}
/**
* `Keccak-p[1600, nr]` 置换函数 / Permutate Function
*
* @param {number} [nr] - 轮数 / Rounds (default: 24)
*/
declare function keccak_p_1600(nr?: number): Keccak_p;
/**
* `SPONGE` 填充函数 / Padding Function
*/
interface SpongePadding {
/**
* @param {Uint8Array} M - 消息 / Message
*/
(M: Uint8Array): Uint8Array;
}
/**
* `SPONGE` & `Keccak-p[1600]`
*
* @param {number} r_byte - 处理速率 / Rate
* @param {number} d_byte - 输出长度 / Digest Size
* @param {SpongePadding} pad - 填充函数 / Padding Function
* @param {Keccak_p} f - Keccak-p 置换函数 / Permutate Function
*/
declare function sponge_1600(r_byte: number, d_byte: number, pad: SpongePadding, f?: Keccak_p): (M: Uint8Array) => U8;
declare const md5: Hash;
declare const sha1: Hash;
declare const sha3_224: Hash;
declare const sha3_256: Hash;
declare const sha3_384: Hash;
declare const sha3_512: Hash;
/**
* @param {number} d - 输出长度 / Digest Size (bit)
*/
declare function shake128(d: number): Hash;
/**
* @param {number} d - 输出长度 / Digest Size (bit)
*/
declare function shake256(d: number): Hash;
/**
* `cSHAKE128` 是 `SHAKE128` 的可定制变体
*
* `cSHAKE128` is a customizable variant of `SHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [N] - 函数名 / Function name
* @param {Uint8Array} [S] - 自定义参数 / Customization
*/
declare function cshake128(d: number, N?: Uint8Array<ArrayBuffer>, S?: Uint8Array<ArrayBuffer>): Hash;
/**
* `cSHAKE256` 是 `SHAKE256` 的可定制变体
*
* `cSHAKE256` is a customizable variant of `SHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} [N] - 函数名 / Function name
* @param {Uint8Array} [S] - 自定义参数 / Customization
*/
declare function cshake256(d: number, N?: Uint8Array<ArrayBuffer>, S?: Uint8Array<ArrayBuffer>): Hash;
/**
* Keccak 消息认证码 (KMAC) 算法
* `KMAC128` 是 `KMAC` 的变体, 由 `cSHAKE128` 构建
*
* The Keccak Message Authentication Code (KMAC) algorithm
* `KMAC128` is a variant of `KMAC`, build from `cSHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac128(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* Keccak 消息认证码 (KMAC) 算法
* `KMAC256` 是 `KMAC` 的变体, 由 `cSHAKE256` 构建
*
* The Keccak Message Authentication Code (KMAC) algorithm
* `KMAC256` is a variant of `KMAC`, build from `cSHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac256(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* 可变长度输出的 `KMAC`
* `KMAC128XOF` 是 `KMAC128` 的 XOF 模式, 由 `cSHAKE128` 构建
*
* `KMAC` with Arbitrary-Length Output
* `KMAC128XOF` is a XOF mode of `KMAC128`, build from `cSHAKE128`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / Recommended key size (bit)
*/
declare function kmac128XOF(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* 可变长度输出的 `KMAC`
* `KMAC256XOF` 是 `KMAC256` 的 XOF 模式, 由 `cSHAKE256` 构建
*
* `KMAC` with Arbitrary-Length Output
* `KMAC256XOF` is a XOF mode of `KMAC256`, build from `cSHAKE256`
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
* @param {number} k_size - 推荐密钥大小 / recommended key size (bit)
*/
declare function kmac256XOF(d: number, S?: Uint8Array<ArrayBuffer>, k_size?: number): KeyHash;
/**
* `TupleHash` 是一个具有可变长度输出的 `SHA3` 派生散列函数, 旨在以一种明确的方式简单地散列输入字符串的元组, 这些字符串中的任何一个或全部都可以是空字符串.
*
* `TupleHash` is a `SHA3` derived hash function with variable-length output that is designed to simply hash a tuple of input strings, any or all of which may be empty strings, in an unambiguous way.
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash128(d: number, S?: Uint8Array): TupleHash;
/**
* `TupleHash` 是一个具有可变长度输出的 `SHA3` 派生散列函数, 旨在以一种明确的方式简单地散列输入字符串的元组, 这些字符串中的任何一个或全部都可以是空字符串.
*
* `TupleHash` is a `SHA3` derived hash function with variable-length output that is designed to simply hash a tuple of input strings, any or all of which may be empty strings, in an unambiguous way.
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash256(d: number, S?: Uint8Array): TupleHash;
/**
* 可变长度输出的 `TupleHash`
*
* `TupleHash` with Arbitrary-Length Output
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash128XOF(d: number, S?: Uint8Array): TupleHash;
/**
* 可变长度输出的 `TupleHash`
*
* `TupleHash` with Arbitrary-Length Output
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function tuplehash256XOF(d: number, S?: Uint8Array): TupleHash;
/**
* `ParallelHash` 的目的是利用现代处理器中可用的并行性, 支持对非常长的字符串进行高效散列.
*
* The purpose of `ParallelHash` is to support the efficient hashing of very long strings, by taking advantage of the parallelism available in modern processors.
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash128(b: number, d: number, S?: Uint8Array): Hash;
/**
* `ParallelHash` 的目的是利用现代处理器中可用的并行性, 支持对非常长的字符串进行高效散列.
*
* The purpose of `ParallelHash` is to support the efficient hashing of very long strings, by taking advantage of the parallelism available in modern processors.
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash256(b: number, d: number, S?: Uint8Array): Hash;
/**
* 可变长度输出的 `ParallelHash`
*
* `ParallelHash` with Arbitrary-Length Output
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash128XOF(b: number, d: number, S?: Uint8Array): Hash;
/**
* 可变长度输出的 `ParallelHash`
*
* `ParallelHash` with Arbitrary-Length Output
*
* @param {number} b - 状态大小 / State size (bit)
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {Uint8Array} S - 自定义参数 / Customization
*/
declare function parallelhash256XOF(b: number, d: number, S?: Uint8Array): Hash;
declare const sha224: Hash;
declare const sha256: Hash;
declare const sha384: Hash;
declare const sha512: Hash;
/**
* @param {number} t - 截断长度 / truncation length (bit)
*/
declare function sha512t(t: number): Hash;
declare const sm3: Hash;
interface TOTP {
/**
* 生成 TOTP (时间同步的一次性密码)
*
* Generate TOTP (Time-based One-Time Password)
*
* @param {Uint8Array} secret - 密钥 / Secret key
* @returns {string} - 返回的 TOTP 字符串 / TOTP string
*/
(secret: Uint8Array): string;
}
interface TOTPParams {
/**
* 带密钥的加密散列算法 / Keyed Hashing Algorithm (default: HMAC-SHA1)
*/
mac?: KeyHash;
/**
* 当前时间戳 / Current timestamp (default: Date.now() milliseconds)
*
* 指定此参数时,将不再从 `Date.now()` 获取当前时间戳.
*
* When this parameter is specified, the current timestamp will not be obtained from `Date.now()`.
*/
current?: number;
/**
* 纪元时间戳 / Epoch timestamp (default: 0 milliseconds)
*/
epoch?: number;
/**
* 时间步长 / Time step (default: 30000 milliseconds)
*/
step?: number;
/**
* 计数器 / Counter
*
* `counter = (cuttent_time - epoch_time) / step`
*
* 指定此参数时,将不再从当前时间戳计算计数器.
*
* When this parameter is specified, the counter will not be calculated from the current timestamp.
*/
counter?: number | bigint | Uint8Array;
/**
* 返回的数字位数 / Number of digits in the returned OTP (default: 6)
*/
digits?: number;
}
/**
* 生成 TOTP (时间同步的一次性密码)
*
* Generate TOTP (Time-based One-Time Password)
*
* @param {Uint8Array} secret - 密钥 / Secret key
* @returns {string} - 返回的 TOTP 字符串 / TOTP string
*/
declare function totp(secret: Uint8Array): string;
/**
* 创建 TOTP 函数 / Create a TOTP function
*
* @param {TOTPParams} params - TOTP 参数 / TOTP parameters
* @returns {TOTP} - 返回的 TOTP 函数 / TOTP function
*/
declare function totp(params: TOTPParams): TOTP;
/**
* TurboSHAKE128
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {number} [D] - 域分隔符 / Domain Separator (range: 0x01 ~ 0x7F, default: 0x1F)
*/
declare function turboshake128(d: number, D?: number): Hash;
/**
* TurboSHAKE256
*
* @param {number} d - 输出长度 / Digest Size (bit)
* @param {number} [D] - 域分隔符 / Domain Separator (range: 0x01 ~ 0x7F, default: 0x1F)
*/
declare function turboshake256(d: number, D?: number): Hash;
export { B32, B64, B64URL, CSV, FpECC, HEX, ISO7816_PAD, NO_PAD, PKCS7_PAD, U8, UTF8, X923_PAD, ZERO_PAD, aes, arc4, arc5, aria, blowfish, bp192r1, bp224r1, bp256r1, bp320r1, bp384r1, bp512r1, camellia, cbc, cfb, createCipher, createHash, createTupleHash, cshake128, cshake256, ctr, curve25519, curve448, des, ecb, eea3, eia3, gcm, genPrime, hkdf, hmac, isProbablePrime, joinBuffer, keccak_p_1600, kmac128, kmac128XOF, kmac256, kmac256XOF, kt128, kt256, md5, mgf1, ofb, p192, p224, p256, p384, p521, parallelhash128, parallelhash128XOF, parallelhash256, parallelhash256XOF, pbkdf2, pcbc, pkcs1_es_1_5, pkcs1_es_oaep, pkcs1_ssa_1_5, pkcs1_ssa_pss, prime192v1, prime256v1, rabbit, rsa, salsa20, scrypt, secp192k1, secp192r1, secp224k1, secp224r1, secp256k1, secp256r1, secp384r1, secp521r1, sha1, sha224, sha256, sha384, sha3_224, sha3_256, sha3_384, sha3_512, sha512, sha512t, shake128, shake256, sm2, sm2p256v1, sm3, sm4, sponge_1600, t_des, tea, totp, tuplehash128, tuplehash128XOF, tuplehash256, tuplehash256XOF, turboshake128, turboshake256, twofish, w25519, w448, x25519, x448, x963kdf, xtea, xxtea, zuc };
export type { BlockCipher, BlockCipherInfo, Cipher, Codec, Digest, ECDSASignature, ECIESCiphertext, ECKeyPair, ECPrivateKey, ECPublicKey, FpECCrypto, FpECPoint, FpMECParams, FpSM2Crypto, FpWECParams, Hash, HashDescription, IVBlockCipher, IVCipher, IVCipherInfo, IVStreamCipher, KDF, KeyDigest, KeyHash, KeyHashDescription, MGF, RSAPrivateKey, RSAPublicKey, RandomPrimeGenerator, SM2DSASignature, StreamCipher, StreamCipherInfo, TupleDigest, TupleHash, TupleHashDescription, X25519, X25519KeyPair, X25519PrivateKey, X25519PublicKey, X448, X448KeyPair, X448PrivateKey, X448PublicKey, XXTEAConfig, ZUCParams };
{
"name": "mima-kit",
"type": "module",
"version": "0.0.17",
"packageManager": "pnpm@9.9.0",
"version": "0.0.18",
"packageManager": "pnpm@10.14.0+sha512.ad27a79641b49c3e481a16a805baa71817a04bbe06a38d17e60e2eaee83f6a146c6a688125f5792e48dd5ba30e7da52a5cda4c3992b9ccf333f9ce223af84748",
"description": "mima-kit is a cryptographic suite implemented in TypeScript. The goal is to provide an easy-to-use cryptographic library. mima-kit 是一个使用 TypeScript 实现的密码学套件。目标是提供一个简单易用的密码学库。",

@@ -30,2 +30,3 @@ "author": "RSoraM",

"hmac",
"totp",
"kmac",

@@ -57,3 +58,4 @@ "blockCipher",

"HKDF",
"PBKDF2"
"PBKDF2",
"scrypt"
],

@@ -101,22 +103,23 @@ "sideEffects": false,

"devDependencies": {
"@antfu/eslint-config": "^2.27.3",
"@antfu/eslint-config": "^5.1.0",
"@antfu/ni": "^0.21.12",
"@testing-library/dom": "^10.4.0",
"@testing-library/dom": "^10.4.1",
"@testing-library/vue": "^8.1.0",
"@types/node": "^22.13.1",
"@vitejs/plugin-vue": "^5.2.1",
"@vitest/browser": "^3.0.5",
"bumpp": "^10.0.2",
"eslint": "^9.20.0",
"@types/node": "^22.17.0",
"@vitejs/plugin-vue": "^5.2.4",
"@vitest/browser": "^3.2.4",
"@vitest/ui": "3.0.5",
"bumpp": "^10.2.2",
"eslint": "^9.32.0",
"esno": "^4.8.0",
"lint-staged": "^15.4.3",
"playwright": "^1.50.1",
"pnpm": "^10.2.1",
"lint-staged": "^15.5.2",
"playwright": "^1.54.2",
"pnpm": "^10.14.0",
"rimraf": "^5.0.10",
"simple-git-hooks": "^2.11.1",
"typescript": "^5.7.3",
"simple-git-hooks": "^2.13.1",
"typescript": "^5.9.2",
"unbuild": "^2.0.0",
"vite": "^6.1.0",
"vitest": "^3.0.5",
"webdriverio": "^9.8.0"
"vite": "^6.3.5",
"vitest": "^3.2.4",
"webdriverio": "^9.18.4"
},

@@ -123,0 +126,0 @@ "simple-git-hooks": {

@@ -152,2 +152,3 @@ <div align="center">

<li><a href="#pbkdf2">PBKDF2</a></li>
<li><a href="#scrypt">Scrypt</a></li>
</ul>

@@ -1728,7 +1729,7 @@ <li><a href="#椭圆曲线列表">椭圆曲线列表</a></li>

/**
* @param {number} k_bit - 期望的密钥长度 / output keying material length
* @param {number} k_byte - 期望的密钥长度 / output keying material length
* @param {Uint8Array} ikm - 输入密钥材料 / input keying material
* @param {Uint8Array} info - 附加信息 / optional context and application specific information
* @param {Uint8Array} salt - 盐 / salt value
*/
(k_bit: number, ikm: Uint8Array, info?: Uint8Array): U8
(k_byte: number, ikm: Uint8Array, salt?: Uint8Array): U8
}

@@ -1739,6 +1740,18 @@ ```

`X9.63KDF` 是 `ANSI-X9.63` 标准中的一个密钥派生函数。`X9.63KDF` 需要组合 `Hash` 函数。
`X9.63KDF` 是 `ANSI-X9.63` 标准中的一个密钥派生函数。
`X9.63KDF` 需要组合 `Hash` 函数和一个可选的 `info`。
对 `X9.63KDF` 输入的 `salt` 将被忽略。
```typescript
const kdf = x963kdf(sha256)
const info = new U8(0)
const kdf = x963kdf(sha256, info)
const k_byte = 64
const ikm = new U8(32)
const salt = new U8(32) // ignore
const k0 = kdf(k_byte, ikm, salt)
const k1 = kdf(k_byte, ikm)
k0 === k1 // true
```

@@ -1748,7 +1761,8 @@

`HKDF` 是 `RFC 5869` 标准中的一个密钥派生函数。`HKDF` 需要组合 `KeyHash` 函数和一个可选的 `salt`
`HKDF` 是 `RFC 5869` 标准中的一个密钥派生函数。`HKDF` 需要组合 `KeyHash` 函数和一个可选的 `info`。
```typescript
const mac = hmac(sha256)
const kdf = hkdf(mac)
const info = new U8(0)
const kdf = hkdf(mac, info)
```

@@ -1758,4 +1772,6 @@

`PBKDF2` 是 `PKCS#5` 标准中的一个密钥派生函数。`PBKDF2` 需要组合 `KeyHash` 函数,指定 `iteration` 次数和一个可选的 `salt`。
`PBKDF2` 是 `PKCS#5` 标准中的一个密钥派生函数。`PBKDF2` 需要组合 `KeyHash` 函数。
默认情况下 `iteration` 为 `1000`。
```typescript

@@ -1766,2 +1782,57 @@ const mac = hmac(sha256)

### Scrypt
`scrypt` 是 `RFC 7914` 标准中的一个密钥派生函数。
`scrypt` 可以指定开销因子、块数、并行因子、最大内存使用量,甚至是内部使用的 `kdf`。
`mima-kit` 提供的 `scrypt` 基于 [`noble-hashes`](https://github.com/paulmillr/noble-hashes) 的实现。
```typescript
const kdf = scrypt()
const config: ScryptConfig = {
N: 16384, // 开销因子
r: 8, // 块数
p: 1, // 并行因子
}
const kdf = scrypt(config)
```
```typescript
interface ScryptConfig {
/**
* 开销因子 / Cost factor (default: 16384)
*
* 必须是 2 的幂
*/
N?: number
/**
* 块数 / Block count (default: 8)
*/
r?: number
/**
* 并行因子 / Parallelization factor (default: 1)
*/
p?: number
/**
* 最大内存使用量 / Maximum memory usage
*
* 如果设置为 0,则不限制内存使用量
*
* (default: 0x40000400 bytes, 1GB + 1KB)
*/
maxmem?: number
/**
* 密钥派生函数 / Key Derivation Function
*
* scrypt 标准使用了 `PBKDF2-HMAC-SHA256` 作为 KDF。
* 该参数允许用户指定其他 KDF,改变 scrypt 的内部行为。
*
* 注意: 这不是 `scrypt` 的标准用法且缺乏相关的安全分析。
*
* (default: pbkdf2(hmac(sha256), 1))
*/
kdf?: KDF
}
```
## 椭圆曲线列表

@@ -1768,0 +1839,0 @@

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display