deno.land / std@0.157.0 / node / _crypto / crypto_browserify / public_encrypt / test / node_test.js
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// Copyright 2017 Calvin Metcalf. All rights reserved. MIT license.
import * as crypto from "../mod.js";import fs from "../../../../fs.ts";import path from "../../../../path.ts";import { Buffer } from "../../../../buffer.ts";import { assertEquals, assertThrows } from "../../../../../testing/asserts.ts";
// Test RSA encryption/decryptionDeno.test("node tests", function () { const keyPem = fs.readFileSync( path.fromFileUrl(new URL("test_key.pem", import.meta.url)), "ascii", ); const rsaPubPem = fs.readFileSync( path.fromFileUrl(new URL("test_rsa_pubkey.pem", import.meta.url)), "ascii", ); const rsaKeyPem = fs.readFileSync( path.fromFileUrl(new URL("test_rsa_privkey.pem", import.meta.url)), "ascii", ); const rsaKeyPemEncrypted = fs.readFileSync( path.fromFileUrl( new URL("test_rsa_privkey_encrypted.pem", import.meta.url), ), "ascii", ); const input = "I AM THE WALRUS"; const bufferToEncrypt = Buffer.from(input);
let encryptedBuffer = crypto.publicEncrypt(rsaPubPem, bufferToEncrypt);
let decryptedBuffer = crypto.privateDecrypt(rsaKeyPem, encryptedBuffer); assertEquals(input, decryptedBuffer.toString());
const decryptedBufferWithPassword = crypto.privateDecrypt({ key: rsaKeyPemEncrypted, passphrase: "password", }, encryptedBuffer); assertEquals(input, decryptedBufferWithPassword.toString());
encryptedBuffer = crypto.publicEncrypt(keyPem, bufferToEncrypt);
decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer); assertEquals(input, decryptedBuffer.toString());
encryptedBuffer = crypto.privateEncrypt(keyPem, bufferToEncrypt);
decryptedBuffer = crypto.publicDecrypt(keyPem, encryptedBuffer); assertEquals(input, decryptedBuffer.toString());
assertThrows(function () { crypto.privateDecrypt({ key: rsaKeyPemEncrypted, passphrase: "wrong", }, encryptedBuffer); });});
Version Info