-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 34a9c15
Showing
5 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
# @nilajs/digital-signature | ||
|
||
A digital signature is a cryptographic mechanism used to verify the authenticity and integrity of digital messages or documents. It provides assurances that the message was indeed created by a particular entity (authentication) and that the message has not been altered or tampered with since it was signed (integrity). | ||
|
||
A simple library for generating elliptic curve (secp256k1) key pairs, signing data, verifying signatures, and validating public keys. | ||
|
||
## Installation | ||
|
||
You can install the package via npm: | ||
|
||
```sh | ||
npm install @nilajs/digital-signature | ||
``` | ||
|
||
## Usage | ||
|
||
### Import the Library | ||
|
||
```javascript | ||
const { | ||
generateWallet, | ||
checkPublicKeyValid, | ||
signData, | ||
verifySignature | ||
} = require('@nilajs/digital-signature'); | ||
``` | ||
|
||
### Generate Wallet | ||
|
||
Generate a new wallet (elliptic curve (secp256k1) key pair) : | ||
|
||
```javascript | ||
const { publicKey, privateKey } = generateWallet(); | ||
console.log(`Public Key: ${publicKey}`); | ||
console.log(`Private Key: ${privateKey}`); | ||
``` | ||
|
||
### Validate Public Key | ||
|
||
Check if a given public key is valid: | ||
|
||
```javascript | ||
const publicKey = 'YOUR_PUBLIC_KEY'; | ||
const result = checkPublicKeyValid(publicKey); | ||
console.log(result); // { valid: true/false, public_key: 'YOUR_PUBLIC_KEY' } | ||
``` | ||
|
||
### Sign Data | ||
|
||
Sign a piece of data with a private key: | ||
|
||
```javascript | ||
const privateKey = 'YOUR_PRIVATE_KEY'; | ||
const data = 'This is the data to be signed'; | ||
const signatureObject = signData(privateKey, data); | ||
console.log(signatureObject); | ||
/* | ||
{ | ||
data: 'This is the data to be signed', | ||
signature: { | ||
r: 'SIGNATURE_R', | ||
s: 'SIGNATURE_S' | ||
} | ||
} | ||
*/ | ||
``` | ||
|
||
### Verify Signature | ||
|
||
Verify the signature of the data with a public key: | ||
|
||
```javascript | ||
const publicKey = 'YOUR_PUBLIC_KEY'; | ||
const data = 'This is the data to be signed'; | ||
const signature = { | ||
r: 'SIGNATURE_R', | ||
s: 'SIGNATURE_S' | ||
}; | ||
const verificationResult = verifySignature(publicKey, data, signature); | ||
console.log(verificationResult); | ||
/* | ||
{ | ||
data: 'This is the data to be signed', | ||
signature: { | ||
r: 'SIGNATURE_R', | ||
s: 'SIGNATURE_S' | ||
}, | ||
is_verified: true/false | ||
} | ||
*/ | ||
``` | ||
|
||
## API Reference | ||
|
||
### `generateWallet()` | ||
|
||
Generates a new elliptic curve (secp256k1) key pair. | ||
|
||
**Returns:** | ||
- `Object`: An object containing the `publicKey` and `privateKey` as hexadecimal strings. | ||
|
||
### `checkPublicKeyValid(publicKey)` | ||
|
||
Checks if a given public key is valid. | ||
|
||
**Parameters:** | ||
- `publicKey` (`string`): The public key to validate. | ||
|
||
**Returns:** | ||
- `Object`: An object containing: | ||
- `valid` (`boolean`): Whether the public key is valid. | ||
- `public_key` (`string`): The provided public key. | ||
|
||
### `signData(privateKey, data)` | ||
|
||
Signs a piece of data with a private key. | ||
|
||
**Parameters:** | ||
- `privateKey` (`string`): The private key to sign with. | ||
- `data` (`string`): The data to sign. | ||
|
||
**Returns:** | ||
- `Object`: An object containing the signed `data` and `signature` (with `r` and `s` as hexadecimal strings). | ||
|
||
### `verifySignature(publicKey, data, signature)` | ||
|
||
Verifies the signature of the data with a public key. | ||
|
||
**Parameters:** | ||
- `publicKey` (`string`): The public key to verify with. | ||
- `data` (`string`): The signed data. | ||
- `signature` (`Object`): The signature object containing `r` and `s` as hexadecimal strings. | ||
|
||
**Returns:** | ||
- `Object`: An object containing: | ||
- `data` (`string`): The signed data. | ||
- `signature` (`Object`): The signature object. | ||
- `is_verified` (`boolean`): Whether the signature is valid. | ||
|
||
## License | ||
|
||
MIT | ||
``` | ||
### Summary | ||
This `README.md` provides an overview of the functions in your `@nilajs/digital-signature` package, along with installation instructions, usage examples, and an API reference. This documentation will help users understand how to use your package effectively. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
Copyright sivabharathy.in All Rights Reserved. | ||
*/ | ||
const crypto = require('crypto'); | ||
const { randomBytes } = require('crypto'); | ||
const { ec } = require('elliptic'); | ||
const EC = new ec('secp256k1'); | ||
/** | ||
* | ||
* @returns | ||
*/ | ||
function generateWallet() { | ||
const privateKey = randomBytes(32).toString('hex'); | ||
const keyPair = EC.keyFromPrivate(privateKey); | ||
const publicKey = keyPair.getPublic('hex'); | ||
return { publicKey, privateKey }; | ||
} | ||
/** | ||
* | ||
* @param {*} publicKey | ||
* @returns Boolean | ||
*/ | ||
function checkPublicKeyValid(publicKey) { | ||
try { | ||
// Attempt to create a keyPair from the provided public key | ||
const keyPair = EC.keyFromPublic(publicKey, 'hex'); | ||
// Validate the key by getting its public key and comparing | ||
const isValid = keyPair.getPublic('hex') === publicKey; | ||
return { valid: isValid, public_key: publicKey } | ||
} catch (error) { | ||
// If an error occurs, the public key is invalid | ||
return { valid: false, public_key: publicKey } | ||
} | ||
} | ||
/** | ||
* | ||
* @param {*} privateKey | ||
* @param {*} data | ||
* @returns Object | ||
*/ | ||
function signData(privateKey, data) { | ||
const keyPair = EC.keyFromPrivate(privateKey); | ||
const msgHash = crypto.createHash('sha256').update(data).digest(); | ||
const signature = keyPair.sign(msgHash); | ||
// return signature with data signed | ||
return { | ||
data, | ||
signature: { | ||
r: signature.r.toString('hex'), | ||
s: signature.s.toString('hex') | ||
} | ||
} | ||
} | ||
/** | ||
* | ||
* @param {*} publicKey | ||
* @param {*} data | ||
* @param {*} signature | ||
* @returns | ||
*/ | ||
function verifySignature(publicKey, data, signature) { | ||
const keyPair = EC.keyFromPublic(publicKey, 'hex'); | ||
const msgHash = crypto.createHash('sha256').update(data).digest(); | ||
|
||
const isVerified = keyPair.verify(msgHash, { | ||
r: signature.r, | ||
s: signature.s | ||
}); | ||
|
||
return { | ||
data, | ||
signature, | ||
is_verified: isVerified | ||
} | ||
} | ||
module.exports = { | ||
generateWallet, | ||
checkPublicKeyValid, | ||
signData, | ||
verifySignature | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "@nilajs/digital-signature", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": ["blockchain", "digital signature"], | ||
"author": "sivabharathy <[email protected]>", | ||
"license": "MIT", | ||
"description": "", | ||
"dependencies": { | ||
"elliptic": "^6.5.5" | ||
} | ||
} |