import * as uint8arrays from "uint8arrays";
import { webcrypto } from "one-webcrypto";
import { importKey, encryptBytes, decryptBytes, makeKey, exportKey } from "keystore-idb/aes/index.js";
import { SymmKeyLength } from "keystore-idb/types.js";
import { impl } from "./implementation.js";
// HASHING
export const sha256Str = async (str) => {
    const hash = await webcrypto.subtle.digest("sha-256", uint8arrays.fromString(str));
    return uint8arrays.toString(new Uint8Array(hash), "hex");
};
export const hash = {
    sha256: async (bytes) => new Uint8Array(await webcrypto.subtle.digest("sha-256", bytes)),
    sha256Str: sha256Str
};
// AES
export const aes = {
    encrypt: async (data, keyStr, alg) => {
        const key = await importKey(keyStr, { length: SymmKeyLength.B256, alg });
        const encrypted = await encryptBytes(data, key, { alg });
        return new Uint8Array(encrypted);
    },
    decrypt: async (encrypted, keyStr, alg) => {
        const key = await importKey(keyStr, { length: SymmKeyLength.B256, alg });
        const decryptedBuf = await decryptBytes(encrypted, key, { alg });
        return new Uint8Array(decryptedBuf);
    },
    genKeyStr: async (alg) => {
        const key = await makeKey({ length: SymmKeyLength.B256, alg });
        return exportKey(key);
    },
    decryptGCM: async (encrypted, keyStr, ivStr) => {
        const iv = uint8arrays.fromString(ivStr, "base64pad");
        const sessionKey = await webcrypto.subtle.importKey("raw", uint8arrays.fromString(keyStr, "base64pad"), "AES-GCM", false, ["encrypt", "decrypt"]);
        // Decrypt secrets
        const decrypted = await webcrypto.subtle.decrypt({
            name: "AES-GCM",
            iv: iv
        }, sessionKey, uint8arrays.fromString(encrypted, "base64pad"));
        return uint8arrays.toString(new Uint8Array(decrypted));
    }
};
// RSA
export const rsa = {
    decrypt: (data, privateKey) => webcrypto.subtle.decrypt({
        name: "RSA-OAEP"
    }, privateKey, data),
    encrypt: async (message, keyStr) => webcrypto.subtle.encrypt({
        name: "RSA-OAEP"
    }, await webcrypto.subtle.importKey("spki", uint8arrays.fromString(keyStr, "base64pad").buffer, { name: "RSA-OAEP", hash: "SHA-256" }, false, ["encrypt"]), message),
    genKey: () => webcrypto.subtle.generateKey({
        name: "RSA-OAEP",
        modulusLength: 2048,
        publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
        hash: { name: "SHA-256" }
    }, true, ["encrypt", "decrypt"]),
    verify: (message, signature, publicKey) => impl.rsa.verify(message, signature, publicKey)
};
// ED25519
export const ed25519 = {
    verify: (message, signature, publicKey) => impl.ed25519.verify(message, signature, publicKey)
};
// KEYSTORE
export const keystore = {
    publicExchangeKey: () => impl.keystore.publicExchangeKey(),
    publicWriteKey: () => impl.keystore.publicWriteKey(),
    decrypt: (encrypted) => impl.keystore.decrypt(encrypted),
    sign: (message, charSize) => impl.keystore.sign(message, charSize),
    importSymmKey: (key, name) => impl.keystore.importSymmKey(key, name),
    exportSymmKey: (name) => impl.keystore.exportSymmKey(name),
    keyExists: (keyName) => impl.keystore.keyExists(keyName),
    getAlg: () => impl.keystore.getAlg(),
    clear: () => impl.keystore.clear()
};
//# sourceMappingURL=index.js.map