import { sm4 } from 'sm-crypto';

export function sm4Decrypt(key, value, mode = "hex") {
    try {
        if (key.length !== 32) {
            uni.showToast({ title: '密钥必须是32位16进制字符串（128位）', icon: 'none' });
            return;
        }
        const decrypted = sm4.decrypt(value, key, {
            mode: 'ecb',
            cipherType: mode === 'hex' ? 'hex' : 'base64',
            padding: 'pkcs#5'
        });
        return decrypted;
    } catch (e) {
        console.error('sm4 decrypt error:', e);
        return value;
    }
}

export function sm4Encrypt(key, value, mode = "hex") {
    try {
        if (key.length !== 32) {
            uni.showToast({ title: '密钥必须是32位16进制字符串（128位）', icon: 'none' });
            return;
        }
        const encrypted = sm4.encrypt(value, key, {
            mode: 'ecb',
            cipherType: mode === 'hex' ? 'hex' : 'base64',
            padding: 'pkcs#5'
        });
        return encrypted;
    } catch (e) {
        console.error('sm4 encrypt error:', e);
        return value;
    }
}
