UNPKG

@tigthor/kokocrypt

Version:

Enhanced Fortress Edition - Secure, quantum-resistant, high-performance encryption package

96 lines 4.35 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.BrowserCryptoService = void 0; const sodium = __importStar(require("libsodium-wrappers")); class BrowserCryptoService { constructor() { } async ready() { await sodium.ready; } async generateKeys() { await sodium.ready; const keypair = sodium.crypto_box_keypair(); return { publicKey: sodium.to_base64(keypair.publicKey), privateKey: sodium.to_base64(keypair.privateKey) }; } async encryptMessage(message, receiverPublicKey, senderPrivateKey) { await sodium.ready; const nonce = sodium.randombytes_buf(sodium.crypto_box_NONCEBYTES); const messageUint8 = sodium.from_string(message); const publicKeyUint8 = sodium.from_base64(receiverPublicKey); const privateKeyUint8 = sodium.from_base64(senderPrivateKey); const encrypted = sodium.crypto_box_easy(messageUint8, nonce, publicKeyUint8, privateKeyUint8); const fullMessage = new Uint8Array(nonce.length + encrypted.length); fullMessage.set(nonce); fullMessage.set(encrypted, nonce.length); return sodium.to_base64(fullMessage); } async decryptMessage(encryptedMessage, senderPublicKey, receiverPrivateKey) { await sodium.ready; const messageUint8 = sodium.from_base64(encryptedMessage); const publicKeyUint8 = sodium.from_base64(senderPublicKey); const privateKeyUint8 = sodium.from_base64(receiverPrivateKey); const nonce = messageUint8.slice(0, sodium.crypto_box_NONCEBYTES); const ciphertext = messageUint8.slice(sodium.crypto_box_NONCEBYTES); const decrypted = sodium.crypto_box_open_easy(ciphertext, nonce, publicKeyUint8, privateKeyUint8); return sodium.to_string(decrypted); } async serverHandshake(serverPublicKey) { await sodium.ready; const clientKeypair = sodium.crypto_box_keypair(); const timestamp = Date.now(); const sessionData = JSON.stringify({ ts: timestamp, client: sodium.to_base64(clientKeypair.publicKey) }); const serverPubKey = sodium.from_base64(serverPublicKey); const nonce = sodium.randombytes_buf(sodium.crypto_box_NONCEBYTES); const encrypted = sodium.crypto_box_easy(sodium.from_string(sessionData), nonce, serverPubKey, clientKeypair.privateKey); const fullMessage = new Uint8Array(nonce.length + encrypted.length); fullMessage.set(nonce); fullMessage.set(encrypted, nonce.length); return { clientPublicKey: sodium.to_base64(clientKeypair.publicKey), clientPrivateKey: sodium.to_base64(clientKeypair.privateKey), encryptedSessionData: sodium.to_base64(fullMessage) }; } } exports.BrowserCryptoService = BrowserCryptoService; exports.default = BrowserCryptoService; //# sourceMappingURL=crypto.browser.js.map