Add mcrypt@0.1

This commit is contained in:
Alan Plum
2018-11-23 17:53:22 +01:00
parent 36a32d8a72
commit 64df3bfb11
4 changed files with 99 additions and 0 deletions

31
types/mcrypt/index.d.ts vendored Normal file
View File

@@ -0,0 +1,31 @@
// Type definitions for mcrypt 0.1
// Project: https://github.com/tugrul/node-mcrypt
// Definitions by: Alan Plum <https://github.com/pluma>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
/// <reference types="node" />
export function getAlgorithmNames(): string[];
export function getModeNames(): string[];
export class MCrypt {
constructor(algorithm: string, mode: string);
open(key: string | Buffer, iv?: string | Buffer): void;
encrypt(plaintext: string | Buffer): Buffer;
decrypt(ciphertext: Buffer): Buffer;
generateIv(): Buffer;
validateKeySize(validate: boolean): void;
validateIvSize(validate: boolean): void;
selfTest(): boolean;
isBlockAlgorithmMode(): boolean;
isBlockAlgorithm(): boolean;
isBlockMode(): boolean;
getBlockSize(): number;
getKeySize(): number;
getSupportedKeySizes(): number[];
getIvSize(): number;
hasIv(): boolean;
getAlgorithmName(): string;
getModeName(): string;
}

View File

@@ -0,0 +1,51 @@
import { getAlgorithmNames, getModeNames, MCrypt } from "mcrypt";
let plaintext: Buffer;
let ciphertext: Buffer;
for (const algo of getAlgorithmNames()) {
console.log(algo.toUpperCase());
}
for (const mode of getModeNames()) {
console.log(mode.toUpperCase());
}
const desEcb = new MCrypt("des", "ecb");
desEcb.open("madepass");
ciphertext = desEcb.encrypt("too many secrets");
console.log(ciphertext.toString("base64"));
plaintext = desEcb.decrypt(ciphertext);
console.log(plaintext.toString());
const blowfishCfb = new MCrypt("blowfish", "cfb");
const iv = blowfishCfb.generateIv();
blowfishCfb.open("somekey", iv);
ciphertext = blowfishCfb.encrypt("sometext");
console.log(Buffer.concat([iv, ciphertext]).toString("base64"));
const bfEcb = new MCrypt("blowfish", "ecb");
bfEcb.validateKeySize(false);
bfEcb.open("typeconfig.sys^_-");
const rjCbc = new MCrypt("rijndael-256", "cbc");
rjCbc.validateIvSize(false);
rjCbc.open("$verysec$retkey$", "foobar");
console.log(blowfishCfb.getBlockSize() * 8);
console.log(blowfishCfb.getKeySize() * 8);
console.log(blowfishCfb.getSupportedKeySizes().map(v => v * 8));
console.log(blowfishCfb.getIvSize() * 8);
console.log(blowfishCfb.getAlgorithmName().toUpperCase());
console.log(blowfishCfb.getModeName().toUpperCase());
function assertBool(value: boolean) {
return value;
}
assertBool(blowfishCfb.selfTest());
assertBool(blowfishCfb.isBlockAlgorithmMode());
assertBool(blowfishCfb.isBlockAlgorithm());
assertBool(blowfishCfb.isBlockMode());
assertBool(blowfishCfb.hasIv());

View File

@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": ["index.d.ts", "mcrypt-tests.ts"]
}

1
types/mcrypt/tslint.json Normal file
View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }