@browser-network/crypto
Advanced tools
Comparing version
@@ -0,7 +1,43 @@ | ||
/** | ||
* @description Buffer to string -- eccrypto uses buffers for everything, and converting them to strings | ||
* makes it easier to use | ||
*/ | ||
export declare const btos: (buffer: Buffer) => string; | ||
/** | ||
* @description String to buffer. Takes a string, like a key, and turns it into a buffer that | ||
* eccrypto can use | ||
*/ | ||
export declare const stob: (str: string) => Buffer; | ||
/** | ||
* @description Generate a hash of any arbitrary data, so long as it's JSON stringifiable | ||
*/ | ||
export declare const hash: (data: any) => Buffer; | ||
export declare const sign: <T>(secret: string, obj: T) => Promise<string>; | ||
/** | ||
* @description Take an object and create a signature for it based on a given private key | ||
*/ | ||
export declare const sign: (secret: string, obj: any) => Promise<string>; | ||
/** | ||
* @description Take an object, signature and pub key, and ensure that the signature matches the object | ||
* given the public key | ||
*/ | ||
export declare const verifySignature: (object: any, signature: string, publicKey: string) => Promise<boolean>; | ||
/** | ||
* @description Derive an EC public key from a given private key | ||
*/ | ||
export declare const derivePubKey: (secret: string) => string; | ||
/** | ||
* @description Generate a new ellyptic curve private key | ||
*/ | ||
export declare const generateSecret: () => string; | ||
/** | ||
* @description Take some data and encrypt it for the supplied public key. The | ||
* owner of that public key, with their associated private key, will be able to | ||
* decrypt the data | ||
*/ | ||
export declare const encrypt: (data: any, toPubKey: string) => Promise<string>; | ||
/** | ||
* @description Take a stringified, encrypted message, as produced by bnc.encrypt, and | ||
* decrypt it using the private key of the associated public key the message was produced | ||
* for | ||
*/ | ||
export declare const decrypt: (message: string, privKey: string) => Promise<any>; |
@@ -71,17 +71,25 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
exports.__esModule = true; | ||
exports.generateSecret = exports.derivePubKey = exports.verifySignature = exports.sign = exports.hash = exports.stob = exports.btos = void 0; | ||
exports.decrypt = exports.encrypt = exports.generateSecret = exports.derivePubKey = exports.verifySignature = exports.sign = exports.hash = exports.stob = exports.btos = void 0; | ||
var crypto_1 = require("crypto"); | ||
var eccrypto = __importStar(require("eccrypto")); | ||
// Buffer to string. eccrypto uses buffers for everything but we want to use strings. | ||
// This is to convert. | ||
/** | ||
* @description Buffer to string -- eccrypto uses buffers for everything, and converting them to strings | ||
* makes it easier to use | ||
*/ | ||
var btos = function (buffer) { return buffer.toString('hex'); }; | ||
exports.btos = btos; | ||
// String to buffer. Takes a string, like a key, and turns it into a buffer that | ||
// eccrypto can use. | ||
/** | ||
* @description String to buffer. Takes a string, like a key, and turns it into a buffer that | ||
* eccrypto can use | ||
*/ | ||
var stob = function (str) { return Buffer.from(str, 'hex'); }; | ||
exports.stob = stob; | ||
// Generate a hash of any arbitrary data, so long as it's JSON stringifiable. | ||
/** | ||
* @description Generate a hash of any arbitrary data, so long as it's JSON stringifiable | ||
*/ | ||
var hash = function (data) { return (0, crypto_1.createHash)('sha256').update(JSON.stringify(data)).digest(); }; | ||
exports.hash = hash; | ||
// Take an object and create a signature for it based on a given private key. | ||
/** | ||
* @description Take an object and create a signature for it based on a given private key | ||
*/ | ||
var sign = function (secret, obj) { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -104,4 +112,6 @@ var stringState, hashBuff, keyBuff, sigBuff, signature; | ||
exports.sign = sign; | ||
// Take an object, signature and pub key, and ensure that the signature matches the object | ||
// given the public key. | ||
/** | ||
* @description Take an object, signature and pub key, and ensure that the signature matches the object | ||
* given the public key | ||
*/ | ||
var verifySignature = function (object, signature, publicKey) { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -131,3 +141,5 @@ var stringObj, hashBuf, pubBuf, sigBuf, e_1; | ||
exports.verifySignature = verifySignature; | ||
// Derive an EC public key from a given private key. | ||
/** | ||
* @description Derive an EC public key from a given private key | ||
*/ | ||
var derivePubKey = function (secret) { | ||
@@ -137,5 +149,5 @@ return (0, exports.btos)(eccrypto.getPublicCompressed((0, exports.stob)(secret))); | ||
exports.derivePubKey = derivePubKey; | ||
// Generate the type of private key that network uses for its | ||
// cryptography. Generating one of these is as good as creating a new identity | ||
// on the network. | ||
/** | ||
* @description Generate a new ellyptic curve private key | ||
*/ | ||
var generateSecret = function () { | ||
@@ -146,3 +158,62 @@ var privBuf = eccrypto.generatePrivate(); | ||
exports.generateSecret = generateSecret; | ||
/** | ||
* @description Take some data and encrypt it for the supplied public key. The | ||
* owner of that public key, with their associated private key, will be able to | ||
* decrypt the data | ||
*/ | ||
var encrypt = function (data, toPubKey) { return __awaiter(void 0, void 0, void 0, function () { | ||
var bufPub, strDat, bufDat, resp, key; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
bufPub = (0, exports.stob)(toPubKey); | ||
strDat = JSON.stringify(data); | ||
bufDat = Buffer.from(strDat) // won't work with hex for some reason | ||
; | ||
return [4 /*yield*/, eccrypto.encrypt(bufPub, bufDat) | ||
// The response here consists of a series of fields that produce an ecies message. | ||
// Each field is a buffer, and we want to string them all up for transport. | ||
// JSON.stringify-ing the whole thing and the parsing it results in a different | ||
// object, so we go piecewise. | ||
]; | ||
case 1: | ||
resp = _a.sent(); | ||
// The response here consists of a series of fields that produce an ecies message. | ||
// Each field is a buffer, and we want to string them all up for transport. | ||
// JSON.stringify-ing the whole thing and the parsing it results in a different | ||
// object, so we go piecewise. | ||
for (key in resp) { | ||
resp[key] = (0, exports.btos)(resp[key]); | ||
} | ||
return [2 /*return*/, JSON.stringify(resp)]; | ||
} | ||
}); | ||
}); }; | ||
exports.encrypt = encrypt; | ||
/** | ||
* @description Take a stringified, encrypted message, as produced by bnc.encrypt, and | ||
* decrypt it using the private key of the associated public key the message was produced | ||
* for | ||
*/ | ||
var decrypt = function (message, privKey) { return __awaiter(void 0, void 0, void 0, function () { | ||
var bufPriv, eciesMessage, key, resp, unbuffed; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
bufPriv = (0, exports.stob)(privKey); | ||
eciesMessage = JSON.parse(message); | ||
// Re-bufferize each field, see note in `encrypt` | ||
for (key in eciesMessage) { | ||
eciesMessage[key] = (0, exports.stob)(eciesMessage[key]); | ||
} | ||
return [4 /*yield*/, eccrypto.decrypt(bufPriv, eciesMessage)]; | ||
case 1: | ||
resp = _a.sent(); | ||
unbuffed = resp.toString(); | ||
return [2 /*return*/, JSON.parse(unbuffed)]; | ||
} | ||
}); | ||
}); }; | ||
exports.decrypt = decrypt; | ||
}); | ||
// TODO encrypt, decrypt, diffie helman | ||
// TODO diffie helman |
{ | ||
"name": "@browser-network/crypto", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A wrapper around eccrypto designed for use in the browser-network ecosystem", | ||
@@ -5,0 +5,0 @@ "main": "./dist/src/index.js", |
@@ -48,1 +48,4 @@ # Browser Network Crypto | ||
``` | ||
or if you're using the UMD build via a <script></script> tag, the `window` object will | ||
automatically be populated with the field `Bnc` which you can use as above, eg. `Bnc.generateSecret()`. |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
1241051
0.68%23385
0.77%51
6.25%