Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

credify-crypto

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

credify-crypto - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

LICENSE

6

dist/encryption.d.ts

@@ -9,3 +9,5 @@ /// <reference types="node" />

constructor();
setKey(privateKeyPem?: string | undefined, publicKeyPem?: string | undefined): Promise<void>;
generateKeyPair(): Promise<void>;
importPrivateKey(pem: string, password?: string | undefined): Promise<void>;
importPublicKey(pem: string): Promise<void>;
encrypt(data: Buffer): Promise<Buffer>;

@@ -15,5 +17,5 @@ encryptStringToBase64String(message: string): Promise<string>;

decryptBase64StringToString(cipher: string): Promise<string>;
exportPrivateKey(): Promise<string>;
exportPrivateKey(password?: string | undefined): Promise<string>;
exportPublicKey(): Promise<string>;
}
export default Encryption;
import { Crypto } from '@peculiar/webcrypto';
import { decomposePrivateKey, composePrivateKey, decomposePublicKey, composePublicKey } from 'crypto-key-composer';
import { pki } from 'node-forge';
import { composePrivateKey, composePublicKey } from 'crypto-key-composer';

@@ -115,12 +115,37 @@ /*! *****************************************************************************

});
Encryption.prototype.setKey = function (privateKeyPem, publicKeyPem) {
if (privateKeyPem === void 0) { privateKeyPem = undefined; }
if (publicKeyPem === void 0) { publicKeyPem = undefined; }
Encryption.prototype.generateKeyPair = function () {
return __awaiter(this, void 0, void 0, function () {
var buffer, key, buffer, key, keys;
var keys;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._crypto.subtle.generateKey({
name: "RSA-OAEP",
hash: "SHA-256",
publicExponent: new Uint8Array([1, 0, 1]),
modulusLength: 4096,
}, false, ["encrypt", "decrypt"])];
case 1:
keys = _a.sent();
this._privateKey = keys.privateKey;
this._publicKey = keys.publicKey;
return [2 /*return*/];
}
});
});
};
Encryption.prototype.importPrivateKey = function (pem, password) {
if (password === void 0) { password = undefined; }
return __awaiter(this, void 0, void 0, function () {
var rawPem, decomposedKey, buffer, key;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!privateKeyPem) return [3 /*break*/, 2];
buffer = extractBuffer(privateKeyPem, "private");
rawPem = pem;
if (password) {
decomposedKey = decomposePrivateKey("\n" + pem + "\n", { password: password, format: "pkcs8-pem" });
// Remove unnecessary property
delete decomposedKey["encryptionAlgorithm"];
rawPem = composePrivateKey(decomposedKey);
}
buffer = extractBuffer(rawPem, "private");
return [4 /*yield*/, this._crypto.subtle.importKey("pkcs8", buffer, {

@@ -133,6 +158,14 @@ name: "RSA-OAEP",

this._privateKey = key;
_a.label = 2;
case 2:
if (!publicKeyPem) return [3 /*break*/, 4];
buffer = extractBuffer(publicKeyPem, "public");
return [2 /*return*/];
}
});
});
};
Encryption.prototype.importPublicKey = function (pem) {
return __awaiter(this, void 0, void 0, function () {
var buffer, key;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
buffer = extractBuffer(pem, "public");
return [4 /*yield*/, this._crypto.subtle.importKey("spki", buffer, {

@@ -142,20 +175,6 @@ name: "RSA-OAEP",

}, true, ["encrypt"])];
case 3:
case 1:
key = _a.sent();
this._publicKey = key;
_a.label = 4;
case 4:
if (!(!privateKeyPem && !publicKeyPem)) return [3 /*break*/, 6];
return [4 /*yield*/, this._crypto.subtle.generateKey({
name: "RSA-OAEP",
hash: "SHA-256",
publicExponent: new Uint8Array([1, 0, 1]),
modulusLength: 4096,
}, false, ["encrypt", "decrypt"])];
case 5:
keys = _a.sent();
this._privateKey = keys.privateKey;
this._publicKey = keys.publicKey;
_a.label = 6;
case 6: return [2 /*return*/];
return [2 /*return*/];
}

@@ -226,5 +245,6 @@ });

};
Encryption.prototype.exportPrivateKey = function () {
Encryption.prototype.exportPrivateKey = function (password) {
if (password === void 0) { password = undefined; }
return __awaiter(this, void 0, void 0, function () {
var data;
var data, rawPem, decomposedKey;
return __generator(this, function (_a) {

@@ -239,3 +259,18 @@ switch (_a.label) {

data = _a.sent();
return [2 /*return*/, pemFormat(Buffer.from(data), "private")];
rawPem = pemFormat(Buffer.from(data), "private");
if (!password) {
return [2 /*return*/, rawPem];
}
decomposedKey = decomposePrivateKey("\n" + rawPem + "\n", { format: "pkcs8-pem" });
// @ts-ignore
decomposedKey["encryptionAlgorithm"] = {
keyDerivationFunc: {
id: 'pbkdf2',
iterationCount: 10000,
keyLength: 32,
prf: 'hmac-with-sha256' // The pseudo-random function
},
encryptionScheme: 'aes256-cbc',
};
return [2 /*return*/, composePrivateKey(decomposedKey, { password: password }).replace(/\n$/, "")];
}

@@ -266,40 +301,3 @@ });

var Signing = /** @class */ (function () {
function Signing(privateKeyPem, publicKeyPem) {
if (privateKeyPem === void 0) { privateKeyPem = undefined; }
if (publicKeyPem === void 0) { publicKeyPem = undefined; }
if (privateKeyPem) {
var buffer = extractBuffer(privateKeyPem, "private");
if (buffer.length === 32) {
var seed = buffer;
var keyPair = pki.ed25519.generateKeyPair({ seed: seed });
this._privateKey = keyPair.privateKey;
this._publicKey = keyPair.publicKey;
}
else if (buffer.length === 48) {
var seed = buffer.slice(16, 48);
var keyPair = pki.ed25519.generateKeyPair({ seed: seed });
this._privateKey = keyPair.privateKey;
this._publicKey = keyPair.publicKey;
}
else {
throw new Error("Buffer size is not incorrect: private key serialization.");
}
}
else if (publicKeyPem) {
var buffer = extractBuffer(publicKeyPem, "public");
if (buffer.length === 32) {
this._publicKey = buffer;
}
else if (buffer.length === 44) {
this._publicKey = buffer.slice(12, 44);
}
else {
throw new Error("Buffer size is not incorrect: public key serialization.");
}
}
else {
var keyPair = pki.ed25519.generateKeyPair();
this._privateKey = keyPair.privateKey;
this._publicKey = keyPair.publicKey;
}
function Signing() {
}

@@ -320,2 +318,19 @@ Object.defineProperty(Signing.prototype, "privateKey", {

});
Signing.prototype.generateKeyPair = function () {
var keyPair = pki.ed25519.generateKeyPair();
this._privateKey = keyPair.privateKey;
this._publicKey = keyPair.publicKey;
};
Signing.prototype.importPrivateKey = function (pem, password) {
if (password === void 0) { password = undefined; }
var decomposedKey = decomposePrivateKey("\n" + pem + "\n", { password: password, format: "pkcs8-pem" });
var seed = decomposedKey.keyData.seed;
var keyPair = pki.ed25519.generateKeyPair({ seed: seed });
this._privateKey = keyPair.privateKey;
this._publicKey = keyPair.publicKey;
};
Signing.prototype.importPublicKey = function (pem) {
var decomposedKey = decomposePublicKey("\n" + pem + "\n");
this._publicKey = decomposedKey.keyData.bytes;
};
Signing.prototype.signStringToBase64 = function (message) {

@@ -332,3 +347,4 @@ return pki.ed25519.sign({ message: message, encoding: "binary", privateKey: this._privateKey }).toString("base64");

};
Signing.prototype.exportPrivateKey = function () {
Signing.prototype.exportPrivateKey = function (password) {
if (password === void 0) { password = undefined; }
return __awaiter(this, void 0, void 0, function () {

@@ -347,3 +363,15 @@ var decomposed;

};
return [2 /*return*/, composePrivateKey(decomposed).replace(/\n$/, "")];
if (password) {
// @ts-ignore
decomposed["encryptionAlgorithm"] = {
keyDerivationFunc: {
id: 'pbkdf2',
iterationCount: 10000,
keyLength: 32,
prf: 'hmac-with-sha256' // The pseudo-random function
},
encryptionScheme: 'aes256-cbc',
};
}
return [2 /*return*/, composePrivateKey(decomposed, { password: password }).replace(/\n$/, "")];
});

@@ -350,0 +378,0 @@ });

@@ -6,4 +6,4 @@ 'use strict';

var webcrypto = require('@peculiar/webcrypto');
var cryptoKeyComposer = require('crypto-key-composer');
var nodeForge = require('node-forge');
var cryptoKeyComposer = require('crypto-key-composer');

@@ -120,12 +120,37 @@ /*! *****************************************************************************

});
Encryption.prototype.setKey = function (privateKeyPem, publicKeyPem) {
if (privateKeyPem === void 0) { privateKeyPem = undefined; }
if (publicKeyPem === void 0) { publicKeyPem = undefined; }
Encryption.prototype.generateKeyPair = function () {
return __awaiter(this, void 0, void 0, function () {
var buffer, key, buffer, key, keys;
var keys;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._crypto.subtle.generateKey({
name: "RSA-OAEP",
hash: "SHA-256",
publicExponent: new Uint8Array([1, 0, 1]),
modulusLength: 4096,
}, false, ["encrypt", "decrypt"])];
case 1:
keys = _a.sent();
this._privateKey = keys.privateKey;
this._publicKey = keys.publicKey;
return [2 /*return*/];
}
});
});
};
Encryption.prototype.importPrivateKey = function (pem, password) {
if (password === void 0) { password = undefined; }
return __awaiter(this, void 0, void 0, function () {
var rawPem, decomposedKey, buffer, key;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!privateKeyPem) return [3 /*break*/, 2];
buffer = extractBuffer(privateKeyPem, "private");
rawPem = pem;
if (password) {
decomposedKey = cryptoKeyComposer.decomposePrivateKey("\n" + pem + "\n", { password: password, format: "pkcs8-pem" });
// Remove unnecessary property
delete decomposedKey["encryptionAlgorithm"];
rawPem = cryptoKeyComposer.composePrivateKey(decomposedKey);
}
buffer = extractBuffer(rawPem, "private");
return [4 /*yield*/, this._crypto.subtle.importKey("pkcs8", buffer, {

@@ -138,6 +163,14 @@ name: "RSA-OAEP",

this._privateKey = key;
_a.label = 2;
case 2:
if (!publicKeyPem) return [3 /*break*/, 4];
buffer = extractBuffer(publicKeyPem, "public");
return [2 /*return*/];
}
});
});
};
Encryption.prototype.importPublicKey = function (pem) {
return __awaiter(this, void 0, void 0, function () {
var buffer, key;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
buffer = extractBuffer(pem, "public");
return [4 /*yield*/, this._crypto.subtle.importKey("spki", buffer, {

@@ -147,20 +180,6 @@ name: "RSA-OAEP",

}, true, ["encrypt"])];
case 3:
case 1:
key = _a.sent();
this._publicKey = key;
_a.label = 4;
case 4:
if (!(!privateKeyPem && !publicKeyPem)) return [3 /*break*/, 6];
return [4 /*yield*/, this._crypto.subtle.generateKey({
name: "RSA-OAEP",
hash: "SHA-256",
publicExponent: new Uint8Array([1, 0, 1]),
modulusLength: 4096,
}, false, ["encrypt", "decrypt"])];
case 5:
keys = _a.sent();
this._privateKey = keys.privateKey;
this._publicKey = keys.publicKey;
_a.label = 6;
case 6: return [2 /*return*/];
return [2 /*return*/];
}

@@ -231,5 +250,6 @@ });

};
Encryption.prototype.exportPrivateKey = function () {
Encryption.prototype.exportPrivateKey = function (password) {
if (password === void 0) { password = undefined; }
return __awaiter(this, void 0, void 0, function () {
var data;
var data, rawPem, decomposedKey;
return __generator(this, function (_a) {

@@ -244,3 +264,18 @@ switch (_a.label) {

data = _a.sent();
return [2 /*return*/, pemFormat(Buffer.from(data), "private")];
rawPem = pemFormat(Buffer.from(data), "private");
if (!password) {
return [2 /*return*/, rawPem];
}
decomposedKey = cryptoKeyComposer.decomposePrivateKey("\n" + rawPem + "\n", { format: "pkcs8-pem" });
// @ts-ignore
decomposedKey["encryptionAlgorithm"] = {
keyDerivationFunc: {
id: 'pbkdf2',
iterationCount: 10000,
keyLength: 32,
prf: 'hmac-with-sha256' // The pseudo-random function
},
encryptionScheme: 'aes256-cbc',
};
return [2 /*return*/, cryptoKeyComposer.composePrivateKey(decomposedKey, { password: password }).replace(/\n$/, "")];
}

@@ -271,40 +306,3 @@ });

var Signing = /** @class */ (function () {
function Signing(privateKeyPem, publicKeyPem) {
if (privateKeyPem === void 0) { privateKeyPem = undefined; }
if (publicKeyPem === void 0) { publicKeyPem = undefined; }
if (privateKeyPem) {
var buffer = extractBuffer(privateKeyPem, "private");
if (buffer.length === 32) {
var seed = buffer;
var keyPair = nodeForge.pki.ed25519.generateKeyPair({ seed: seed });
this._privateKey = keyPair.privateKey;
this._publicKey = keyPair.publicKey;
}
else if (buffer.length === 48) {
var seed = buffer.slice(16, 48);
var keyPair = nodeForge.pki.ed25519.generateKeyPair({ seed: seed });
this._privateKey = keyPair.privateKey;
this._publicKey = keyPair.publicKey;
}
else {
throw new Error("Buffer size is not incorrect: private key serialization.");
}
}
else if (publicKeyPem) {
var buffer = extractBuffer(publicKeyPem, "public");
if (buffer.length === 32) {
this._publicKey = buffer;
}
else if (buffer.length === 44) {
this._publicKey = buffer.slice(12, 44);
}
else {
throw new Error("Buffer size is not incorrect: public key serialization.");
}
}
else {
var keyPair = nodeForge.pki.ed25519.generateKeyPair();
this._privateKey = keyPair.privateKey;
this._publicKey = keyPair.publicKey;
}
function Signing() {
}

@@ -325,2 +323,19 @@ Object.defineProperty(Signing.prototype, "privateKey", {

});
Signing.prototype.generateKeyPair = function () {
var keyPair = nodeForge.pki.ed25519.generateKeyPair();
this._privateKey = keyPair.privateKey;
this._publicKey = keyPair.publicKey;
};
Signing.prototype.importPrivateKey = function (pem, password) {
if (password === void 0) { password = undefined; }
var decomposedKey = cryptoKeyComposer.decomposePrivateKey("\n" + pem + "\n", { password: password, format: "pkcs8-pem" });
var seed = decomposedKey.keyData.seed;
var keyPair = nodeForge.pki.ed25519.generateKeyPair({ seed: seed });
this._privateKey = keyPair.privateKey;
this._publicKey = keyPair.publicKey;
};
Signing.prototype.importPublicKey = function (pem) {
var decomposedKey = cryptoKeyComposer.decomposePublicKey("\n" + pem + "\n");
this._publicKey = decomposedKey.keyData.bytes;
};
Signing.prototype.signStringToBase64 = function (message) {

@@ -337,3 +352,4 @@ return nodeForge.pki.ed25519.sign({ message: message, encoding: "binary", privateKey: this._privateKey }).toString("base64");

};
Signing.prototype.exportPrivateKey = function () {
Signing.prototype.exportPrivateKey = function (password) {
if (password === void 0) { password = undefined; }
return __awaiter(this, void 0, void 0, function () {

@@ -352,3 +368,15 @@ var decomposed;

};
return [2 /*return*/, cryptoKeyComposer.composePrivateKey(decomposed).replace(/\n$/, "")];
if (password) {
// @ts-ignore
decomposed["encryptionAlgorithm"] = {
keyDerivationFunc: {
id: 'pbkdf2',
iterationCount: 10000,
keyLength: 32,
prf: 'hmac-with-sha256' // The pseudo-random function
},
encryptionScheme: 'aes256-cbc',
};
}
return [2 /*return*/, cryptoKeyComposer.composePrivateKey(decomposed, { password: password }).replace(/\n$/, "")];
});

@@ -355,0 +383,0 @@ });

@@ -7,8 +7,10 @@ /// <reference types="node" />

get publicKey(): Buffer;
constructor(privateKeyPem?: string | undefined, publicKeyPem?: string | undefined);
generateKeyPair(): void;
importPrivateKey(pem: string, password?: string | undefined): void;
importPublicKey(pem: string): void;
signStringToBase64(message: string): string;
verifyString(message: string, signature: string): boolean;
exportPrivateKey(): Promise<string>;
exportPrivateKey(password?: string | undefined): Promise<string>;
exportPublicKey(): Promise<string>;
}
export default Signing;
{
"name": "credify-crypto",
"version": "0.1.0",
"version": "0.2.0",
"description": "Credify cryptographic related helpers in JavaScript",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -5,5 +5,59 @@ # CredifyCryptoJS

## How to install
```shell script
// yarn
$ yarn add credify-crypto
// npm
$ npm install credify-crypto
```
## How to use
### Encryption
```typescript
import { Encryption } from "credify-crypto";
const privateKeyPem =
`-----BEGIN PRIVATE KEY-----
MIIJQwIBADANBgkqhkiG9w0BAQEFAASCCS0wggkpAgEAAoICAQDrEavVupOf+G7U
83oh0TKLK0U+9BdY4cgPCe66gyLodY9NF7+r9NgZHlsSOaXXTCEJOo8/rb+JFW34
....
agUydf7cXbP1YoOBizTgt/WBiQk47Ym4JvEWDTHmEsT4snYjujiMvAlAS37JyvWD
osQZlO5eDVv0XR3aNy5pOR/ouC0o8a8=
-----END PRIVATE KEY-----`;
const cipherText = "XGI1icHgMVuTvYLMEwXp...YCfEqkHxv5FdyWaw="; // base64 encoded string
const encryptionSample = async () => {
const encryption = new Encryption();
await encryption.importPrivateKey(privateKeyPem);
const plainText = await encryption.decryptBase64StringToString(cipherText);
console.log(plainText);
}
```
### Signing
```typescript
import { Signing } from "credify-crypto";
const privateKeyPem = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIPqO4b4UtXSaWGp5u38rCXYu4/LdbaSk7lD46LtRUu44
-----END PRIVATE KEY-----`;
const message = "test message";
const signing = new Signing();
signing.importPrivateKey(privateKeyPem);
const sign = signing.signStringToBase64(message);
const verified = signing.verifyString(message, sign);
console.log(verified) // true
```
## Asymmetric Encryption
CredifyCryptoSwift supports RSA encryption with 4096 bit length keys. Its padding scheme uses [OAEP](https://en.wikipedia.org/wiki/Optimal_asymmetric_encryption_padding).
CredifyCryptoJS supports RSA encryption with 4096 bit length keys. Its padding scheme uses [OAEP](https://en.wikipedia.org/wiki/Optimal_asymmetric_encryption_padding).

@@ -10,0 +64,0 @@ ## Signing

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc