@li0ard/magma
Version:
Magma cipher implementation in pure TypeScript
31 lines (30 loc) • 1.4 kB
JavaScript
import { Magma, sboxes, BLOCK_SIZE } from "../index.js";
import { ecb_encrypt, ecb_decrypt } from "@li0ard/gost3413";
/**
* Encrypts data using Electronic Codebook (ECB) mode with Magma cipher.
*
* @param key Encryption key
* @param data Data to be encrypted
* @param legacy Enable backward compatibility with old GOST 28147-89
* @param sbox Optional substitution box, defaults to `ID_TC26_GOST_28147_PARAM_Z`
* @returns {Uint8Array}
*/
export const encryptECB = (key, data, legacy = false, sbox = sboxes.ID_TC26_GOST_28147_PARAM_Z) => {
const cipher = new Magma(legacy ? Magma.reverseKey(key) : key, sbox);
const result = ecb_encrypt((legacy ? cipher.encryptLegacy : cipher.encryptBlock).bind(cipher), BLOCK_SIZE, data);
return result;
};
/**
* Decrypts data using Electronic Codebook (ECB) mode with Magma cipher.
*
* @param key Encryption key
* @param data Data to be decrypted
* @param legacy Enable backward compatibility with old GOST 28147-89
* @param sbox Optional substitution box, defaults to `ID_TC26_GOST_28147_PARAM_Z`
* @returns {Uint8Array}
*/
export const decryptECB = (key, data, legacy = false, sbox = sboxes.ID_TC26_GOST_28147_PARAM_Z) => {
const cipher = new Magma(legacy ? Magma.reverseKey(key) : key, sbox);
const result = ecb_decrypt((legacy ? cipher.decryptLegacy : cipher.decryptBlock).bind(cipher), BLOCK_SIZE, data);
return result;
};