How to use the crypt.BlockCipher function in crypt

To help you get started, we’ve selected a few crypt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Moddable-OpenSource / moddable / examples / crypt / cryptblockcipher / main.js View on Github external
function testBlockCipher(name, keyStr, plain, expected)
{
	let key = ("string" == typeof keyStr) ? ArrayBuffer.fromString(keyStr) : keyStr;
	expected = Hex.toBuffer(expected);

	let result = (new BlockCipher(name, key)).encrypt(plain + "");

	if (Bin.comp(result, expected) != 0)
		trace(`${name} FAIL\n`);
	else
		trace(`${name} pass\n`);

	plain = ArrayBuffer.fromString(plain);
	result = (new BlockCipher(name, key)).decrypt(result)
	
	if (Bin.comp(result, plain) != 0) {
		trace(`${name} FAIL\n`);
		trace("result = " + Hex.toString(result) + "\n");
		trace("plain = " + Hex.toString(plain) + "\n");
	}
	else
		trace(`${name} pass\n`);
}
github Moddable-OpenSource / moddable / modules / crypt / ssl / ssl_setup.js View on Github external
function setupSub(o, cipher)
{
	let enc, h;

	switch (cipher.cipherAlgorithm) {
	case DES:
		enc = new BlockCipher("DES", o.key);
		break;
	case TDES:
		enc = new BlockCipher("TDES", o.key);
		break;
	case AES:
		enc = new BlockCipher("AES", o.key);
		break;
	case RC4:
		enc = new StreamCipher("RC4", o.key);
		break;
	default:
		throw new Error("SSL: SetupCipher: unkown encryption algorithm");
	}
	switch (cipher.encryptionMode) {
	case CBC:
	case NONE:
github Moddable-OpenSource / moddable / examples / crypt / cryptblockcipher / main.js View on Github external
function testBlockCipher(name, keyStr, plain, expected)
{
	let key = ("string" == typeof keyStr) ? ArrayBuffer.fromString(keyStr) : keyStr;
	expected = Hex.toBuffer(expected);

	let result = (new BlockCipher(name, key)).encrypt(plain + "");

	if (Bin.comp(result, expected) != 0)
		trace(`${name} FAIL\n`);
	else
		trace(`${name} pass\n`);

	plain = ArrayBuffer.fromString(plain);
	result = (new BlockCipher(name, key)).decrypt(result)
	
	if (Bin.comp(result, plain) != 0) {
		trace(`${name} FAIL\n`);
		trace("result = " + Hex.toString(result) + "\n");
		trace("plain = " + Hex.toString(plain) + "\n");
	}
	else
		trace(`${name} pass\n`);
github Moddable-OpenSource / moddable / modules / crypt / ssl / ssl_setup.js View on Github external
function setupSub(o, cipher)
{
	let enc, h;

	switch (cipher.cipherAlgorithm) {
	case DES:
		enc = new BlockCipher("DES", o.key);
		break;
	case TDES:
		enc = new BlockCipher("TDES", o.key);
		break;
	case AES:
		enc = new BlockCipher("AES", o.key);
		break;
	case RC4:
		enc = new StreamCipher("RC4", o.key);
		break;
	default:
		throw new Error("SSL: SetupCipher: unkown encryption algorithm");
	}
	switch (cipher.encryptionMode) {
	case CBC:
	case NONE:
		switch (cipher.hashAlgorithm) {
		case MD5: h = "MD5"; break;
		case SHA1: h = "SHA1"; break;
		case SHA256: h = "SHA256"; break;
		case SHA384: h = "SHA384"; break;
		default:
github Moddable-OpenSource / moddable / examples / crypt / cryptblockcipher / main.js View on Github external
import Bin from "bin";

let Hex = {
	toBuffer(s) {
		return ArrayBuffer.fromBigInt(BigInt("0x" + s), s.length / 2);
	},
	toString(b) {
		return BigInt.fromArrayBuffer(b).toString(16);
	},
};

testCipherMode("ECB", "decrypt", new BlockCipher("DES", ArrayBuffer.fromString("ABCDEFGH")), undefined, undefined, Hex.toBuffer("5BD7DE165BB69D60D04399DDAD80227B"), ArrayBuffer.fromString("0123456789abcdef"));
testCipherMode("ECB", "encrypt", new BlockCipher("DES", ArrayBuffer.fromString("ABCDEFGH")), undefined, undefined, ArrayBuffer.fromString("0123456789abcdef"), Hex.toBuffer("5BD7DE165BB69D60D04399DDAD80227B"));


testCipherMode("CBC", "decrypt", new BlockCipher("DES", ArrayBuffer.fromString("ABCDEFGH")), undefined, undefined, Hex.toBuffer("77DEDD68ED79D710CA4E42C674850E61"), ArrayBuffer.fromString("this is a test!!"));
testCipherMode("CBC", "encrypt", new BlockCipher("DES", ArrayBuffer.fromString("ABCDEFGH")), undefined, undefined, ArrayBuffer.fromString("this is a test!!"), Hex.toBuffer("77DEDD68ED79D710CA4E42C674850E61"));

testCipherMode("CTR", "decrypt", new BlockCipher("AES", Hex.toBuffer("AE6852F8121067CC4BF7A5765577F39E")),
				Hex.toBuffer("00000030000000000000000000000001"), undefined, Hex.toBuffer("E4095D4FB7A7B3792D6175A3261311B8"), ArrayBuffer.fromString("Single block msg"));

testCipherMode("CTR", "decrypt", new BlockCipher("AES", Hex.toBuffer("7E24067817FAE0D743D6CE1F32539163")),
				Hex.toBuffer("006CB6DBC0543B59DA48D90B00000001"), undefined,
				Hex.toBuffer("5104A106168A72D9790D41EE8EDAD388EB2E1EFC46DA57C8FCE630DF9141BE28"),
				Hex.toBuffer("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"));

testCipherMode("CTR", "encrypt", new BlockCipher("AES", Hex.toBuffer("7E24067817FAE0D743D6CE1F32539163")),
				Hex.toBuffer("006CB6DBC0543B59DA48D90B00000001"), undefined,
				Hex.toBuffer("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"),
				Hex.toBuffer("5104A106168A72D9790D41EE8EDAD388EB2E1EFC46DA57C8FCE630DF9141BE28"));

testBlockCipher("DES", "ABCDEFGH", "01234567", "5BD7DE165BB69D60");