gmkitx API - v0.10.1
    正在准备搜索索引...

    变量 sm4常量

    sm4: {
        SM4: typeof SM4;
        decrypt(
            key: BytesLike,
            encryptedData: BytesLike | SM4CipherResult,
            options?: SM4DecryptOptions,
        ): string;
        decryptBytes(
            key: BytesLike,
            encryptedData: BytesLike | SM4CipherResult,
            options?: SM4DecryptOptions,
        ): Uint8Array;
        encrypt(
            key: BytesLike,
            data: string | Uint8Array<ArrayBufferLike>,
            options?: SM4Options,
        ): SM4CipherResult;
    } = ...

    SM4 分组密码算法模块。 聚合所有具名函数与对象式入口 SM4Class

    类型声明

    • SM4: typeof SM4

      SM4 对象式 API,实例保存 key、mode、padding 与可选 IV 配置。

    • decrypt: function
      • 使用 SM4 解密数据 Decrypt data using SM4 block cipher

        支持的模式 (Supported modes):

        • ECB: 电码本模式 (Electronic Codebook)
        • CBC: 分组链接模式 (Cipher Block Chaining) - 需要IV (Requires IV)
        • CTR: 计数器模式 (Counter mode) - 流密码模式,需要IV (Stream mode, requires IV)
        • CFB: 密文反馈模式 (Cipher Feedback) - 流密码模式,需要IV (Stream mode, requires IV)
        • OFB: 输出反馈模式 (Output Feedback) - 流密码模式,需要IV (Stream mode, requires IV)
        • GCM: 伽罗瓦/计数器模式 (Galois/Counter Mode) - 认证加密,需要IV和tag (AEAD mode, requires IV and tag)
        • CCM: 计数器与 CBC-MAC 模式 (Counter with CBC-MAC) - 认证加密,需要 nonce 和 tag (AEAD mode, requires nonce and tag)

        支持的填充模式 (Supported padding modes):

        • PKCS7: PKCS#7 填充 (PKCS#7 padding) - 默认 (Default)
        • NONE: 无填充 (No padding)
        • ZERO: 零填充 (Zero padding)

        参数

        • key: BytesLike

          解密密钥(十六进制字符串,32 个字符 = 16 字节) Decryption key (hex string, 32 chars = 16 bytes)

        • encryptedData: BytesLike | SM4CipherResult

          加密的数据(十六进制字符串或 AEAD 结果对象) Encrypted data (hex string or AEAD result object)

        • 可选options: SM4DecryptOptions

          解密选项(模式、填充、IV、tag用于 GCM/CCM) Decryption options (mode, padding, IV, tag for GCM/CCM)

        返回 string

        解密后的数据(UTF-8 字符串) Decrypted data (UTF-8 string)

        // ECB 模式
        const decrypted = decrypt(key, encrypted, { mode: CipherMode.ECB, padding: PaddingMode.PKCS7 });
        // GCM 模式(校验认证标签)
        const decrypted = decrypt(key, result, { mode: CipherMode.GCM, iv: '000000000000000000000000', aad: 'metadata' });
    • decryptBytes: function
      • 使用 SM4 解密为原始字节;文本入口会额外执行 UTF-8 解码。

        参数

        • key: BytesLike

          固定 16 字节的密钥或 32 个 Hex 字符

        • encryptedData: BytesLike | SM4CipherResult

          字符串、原始密文字节或带 tag 的结构化结果

        • 可选options: SM4DecryptOptions

          mode、padding、IV/nonce、AAD、tag 与输入编码

        返回 Uint8Array

        解密并完成填充/AEAD 校验后的原始字节

        参数长度、编码、padding 或 AEAD 认证无效时抛出错误

    • encrypt: function
      • 使用 SM4 加密数据 Encrypt data using SM4 block cipher

        支持的模式 (Supported modes):

        • ECB: 电码本模式 (Electronic Codebook) - 不推荐用于生产环境 (Not recommended for production)
        • CBC: 分组链接模式 (Cipher Block Chaining) - 需要IV (Requires IV)
        • CTR: 计数器模式 (Counter mode) - 流密码模式,需要IV (Stream mode, requires IV)
        • CFB: 密文反馈模式 (Cipher Feedback) - 流密码模式,需要IV (Stream mode, requires IV)
        • OFB: 输出反馈模式 (Output Feedback) - 流密码模式,需要IV (Stream mode, requires IV)
        • GCM: 伽罗瓦/计数器模式 (Galois/Counter Mode) - 认证加密,需要IV (AEAD mode, requires IV)
        • CCM: 计数器与 CBC-MAC 模式 (Counter with CBC-MAC) - 认证加密,需要 nonce (AEAD mode, requires nonce)

        支持的填充模式 (Supported padding modes):

        • PKCS7: PKCS#7 填充 (PKCS#7 padding) - 默认 (Default)
        • NONE: 无填充 (No padding) - 数据长度必须是16字节的倍数 (Data length must be multiple of 16 bytes)
        • ZERO: 零填充 (Zero padding) - 用零字节填充 (Pad with zero bytes)

        参数

        • key: BytesLike

          加密密钥(十六进制字符串,32 个字符 = 16 字节) Encryption key (hex string, 32 chars = 16 bytes)

        • data: string | Uint8Array<ArrayBufferLike>

          要加密的数据(字符串或 Uint8Array) Data to encrypt (string or Uint8Array)

        • 可选options: SM4Options

          加密选项(模式、填充、IV) Encryption options (mode, padding, IV)

        返回 SM4CipherResult

        小写十六进制字符串形式的加密数据,或GCM模式下返回包含密文和标签的对象 Encrypted data as lowercase hex string, or object with ciphertext and tag for GCM mode

        // ECB 模式
        const encrypted = encrypt(key, 'order=GMKIT-DEMO-0001&amount=88.00', { mode: CipherMode.ECB, padding: PaddingMode.PKCS7 });
        // GCM 模式(包含认证标签)
        const result = encrypt(key, 'order=GMKIT-DEMO-0001&amount=88.00', { mode: CipherMode.GCM, iv: '000000000000000000000000', aad: 'tenant=demo;schema=1' });
        console.log(result.ciphertext, result.tag);