Skip to content

Commit

Permalink
Add documentation and Github Actions (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
PlamenHristov authored May 21, 2023
1 parent cdd9ed4 commit f295955
Show file tree
Hide file tree
Showing 11 changed files with 512 additions and 222 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Pull Request Checks

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node-version: [16.x, 18.x, 20.x]

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }} ${{ matrix.os }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm ci

- name: Run tests
run: npm test

- name: Run build
run: npm run build

- name: Coveralls Parallel
uses: coverallsapp/github-action@v2
with:
flag-name: run-${{ join(matrix.*, '-') }}
parallel: true

finish:
needs: build
if: ${{ always() }}
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@v2
with:
parallel-finished: true
carryforward: "run-1,run-2"
30 changes: 30 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Release new package version

on:
push:
tags:
- 'v*'

jobs:
publish-npm:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: 16.x
registry-url: 'https://registry.npmjs.org'

- name: Install dependencies
run: npm ci

- name: Build
run: npm run build

- name: Publish
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
164 changes: 163 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,164 @@
# @blocksoft/cryptomate
NodeJS crypto module wrapper for easier use

Ergonomic, zero-dependency crypto module wrapper for ECDSA and EdDSA signatures.

[![Pull Request Checks](https://github.com/PlamenHristov/cryptomate/actions/workflows/pr.yml/badge.svg?branch=main&event=release)](https://github.com/PlamenHristov/cryptomate/actions/workflows/pr.yml)
[![Coverage Status](https://coveralls.io/repos/github/PlamenHristov/cryptomate/badge.svg?branch=main)](https://coveralls.io/github/PlamenHristov/cryptomate?branch=main)
[![Known Vulnerabilities](https://snyk.io/test/github/PlamenHristov/cryptomate/badge.svg?targetFile=package.json)](https://snyk.io/test/github/PlamenHristov/cryptomate?targetFile=package.json)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Key features of the module include:

- Generation of ECDSA and EdDSA key pairs.
- Conversion between PEM (Privacy-Enhanced Mail) and DER (Distinguished Encoding Rules) formats.
- Extraction of public key from a given private key.
- Message signing and verification using ECDSA and EdDSA algorithms.

## Installation

To install `@blocksoft/cryptomate`, use the following npm command:

```bash
npm i --save @blocksoft/cryptomate
```

## Quick Start

Here is an example demonstrating the use of the ECDSA functionality provided by this module:

```javascript
const {ECDSA, EC_CURVE, Key, SignatureEncoding} = require('@blocksoft/cryptomate');

// Generate an ECDSA key pair.
const ecdsa = ECDSA.withCurve(EC_CURVE.secp256k1).genKeyPair();

// Sign a message.
let message = "Hello, World!";
let signature = ecdsa.sign(message, SignatureEncoding.HEX);

// Verify the signature.
console.log(ecdsa.verify(message, signature)); // Outputs: true

// Export keys in PEM format.
let privateKeyPEM = ecdsa.toPEM(Key.privateKey);
let publicKeyPEM = ecdsa.toPEM(Key.publicKey);

// Import keys from PEM format.
let importedECDSA = ECDSA.withCurve(EC_CURVE.secp256k1).fromPEM(privateKeyPEM, Key.privateKey);

```

## API Reference

### ECDSA

#### ECDSA.withCurve(curve)

A factory method to construct an ECDSA object with a given elliptic curve.

##### Parameters

- `curve` - The elliptic curve to use. This can be one of the following values:
- `EC_CURVE.P_256` - NIST P-256 curve.
- `EC_CURVE.P_384` - NIST P-384 curve.
- `EC_CURVE.P_521` - NIST P-521 curve.
- `EC_CURVE.SECP256K1` - SECP256K1 curve.
- `EC_CURVE.SECP256R1` - SECP256R1 curve.
- `EC_CURVE.SECP384R1` - SECP384R1 curve.
- `EC_CURVE.SECP521R1` - SECP521R1 curve.
- ...

##### Returns

An ECDSA object with the given elliptic curve.

#### ECDSA.genKeyPair()

Generates a new ECDSA key pair.

##### Returns

An ECDSA object with a newly generated key pair.

#### ECDSA.fromPEM(pem, keyType)

Constructs an ECDSA object from a given PEM string.

##### Parameters

- `pem` - The PEM string to construct the ECDSA object from.
- `keyType` - The type of key to construct. This can be one of the following values:
- `Key.privateKey` - Private key.
- `Key.publicKey` - Public key.

##### Returns

An ECDSA object with the given PEM string.

#### ECDSA.toPEM(keyType)

Converts the ECDSA object to a PEM string.

##### Parameters

- `keyType` - The type of key to convert. This can be one of the following values:
- `Key.privateKey` - Private key.
- `Key.publicKey` - Public key.

##### Returns

A PEM string representing the ECDSA object.

#### ECDSA.getPublicKey()

Extracts the public key from the ECDSA object.

##### Returns

A Buffer object containing the public key.

#### ECDSA.sign(message, encoding)

Signs a message using the ECDSA object.

##### Parameters

- `message` - The message to sign.
- `encoding` - The encoding of the message. This can be one of the following values:
- `SignatureEncoding.HEX` - The message is encoded in hexadecimal format.
- `SignatureEncoding.BASE64` - The message is encoded in Base64 format.
- `SignatureEncoding.UTF8` - The message is encoded in UTF-8 format.
- ...

##### Returns

A Buffer object containing the signature.

#### ECDSA.verify(message, signature, encoding)

Verifies a signature using the ECDSA object.

##### Parameters

- `message` - The message to verify.
- `signature` - The signature to verify.
- `encoding` - The encoding of the message. This can be one of the following values:
- `SignatureEncoding.HEX` - The message is encoded in hexadecimal format.
- `SignatureEncoding.BASE64` - The message is encoded in Base64 format.
- `SignatureEncoding.UTF8` - The message is encoded in UTF-8 format.
- ...

##### Returns

A boolean value indicating whether the signature is valid.

### EdDSA

Please note that the EdDSA class shares similar method names and functionalities to the ECDSA class for key pair
generation,
signing and verifying messages, and importing/exporting keys in various formats.
Refer to the ECDSA API documentation provided above for further details.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
32 changes: 27 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "test"
},
"scripts": {
"test": "jest",
"test": "jest --coverage",
"clean": "rm -rf ./dist",
"build": "npm run clean && npm run build:esm && npm run build:cjs",
"build:esm": "tsc -p ./configs/tsconfig.esm.json && mv dist/esm/index.js dist/esm/index.mjs",
Expand All @@ -33,6 +33,10 @@
"type": "git",
"url": "git+https://github.com/PlamenHristov/cryptomate.git"
},
"engines": {
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"keywords": [
"nodejs",
"crypto",
Expand All @@ -58,10 +62,25 @@
"plugin:@typescript-eslint/recommended"
],
"rules": {
"semi": ["error", "never"],
"indent": ["error", 2],
"quotes": ["error", "double"],
"no-multiple-empty-lines": [2, {"max": 99999, "maxEOF": 0}]
"semi": [
"error",
"never"
],
"indent": [
"error",
2
],
"quotes": [
"error",
"double"
],
"no-multiple-empty-lines": [
2,
{
"max": 99999,
"maxEOF": 0
}
]
},
"env": {
"browser": true,
Expand All @@ -72,5 +91,8 @@
"license": "MIT",
"bugs": {
"url": "https://github.com/PlamenHristov/cryptomate/issues"
},
"volta": {
"node": "20.2.0"
}
}
Loading

0 comments on commit f295955

Please sign in to comment.