Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/keng42/cnigma
Facebook Conceal and RSA utilities for JS
https://github.com/keng42/cnigma
Last synced: 1 day ago
JSON representation
Facebook Conceal and RSA utilities for JS
- Host: GitHub
- URL: https://github.com/keng42/cnigma
- Owner: keng42
- Created: 2019-02-15T09:50:06.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-23T10:37:40.000Z (over 4 years ago)
- Last Synced: 2024-11-07T09:08:10.692Z (11 days ago)
- Language: TypeScript
- Homepage:
- Size: 54.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Facebook Conceal and RSA utilities for js
Compatible with [Facebook Conceal 2.0](https://github.com/facebook/conceal)
## Usage
### Conceal
```js
const { Conceal } = require('cnigma');
const conceal = new Conceal('my-password');// or
const conceal = new Conceal('my-password', myKey, encoding);
// encoding: hex or base64
// myKey: 32 bytes with ${encoding} encoded string
```### String
```js
const plain = 'hello world @ 2020';
const encrypted = conceal.encryptStr(plain);
const decrypted = conceal.decryptStr(encrypted);
```### File
```js
const srcFilePath = 'test/xxy007.png';
const encFilePath = 'test/xxy007.png.enc';
const decFilePath = 'test/xxy007.dec.png';conceal
.encryptFile(path, encFilePath)
.then(() => conceal.decryptFile(encFilePath, decFilePath))
.then(() => {
console.log('done');
})
.catch((err) => {
console.log(err);
});
```### Using in browser
```html
const { Conceal, Rsa } = require('cnigma');
const conceal = new Conceal('my-password');(async () => {
const plain = 'hello world @ 2020';
const encrypted = await conceal.encryptStr(plain);
const decrypted = await conceal.decryptStr(encrypted);
})();```
### RSA
```js
const { Rsa } = require('cnigma');const privateKey = fs.readFileSync('rsa-private.pem', 'utf8');
const publicKey = fs.readFileSync('rsa-public.pem', 'utf8');const rsa = new Rsa(rsaPrivateKey, rsaPublicKey, 'base64');
const plain = 'hello world @ 2020';
const signed = rsa.sign(plain);
const verified = rsa.verify(plain, signed);
const encrypted = rsa.encrypt(plain);
const decrypted = rsa.decrypt(encrypted);
```## FAQ
### Private Key
[The header `-----BEGIN RSA PRIVATE KEY-----` in the PEM file is reserved to PKCS#1 keys, but WebCryptoApi does not support pkcs1, so you need to convert the key from pkcs1 to pkcs8 using a tool like openssl](https://stackoverflow.com/questions/51033786/how-can-i-import-an-rsa-private-key-in-pem-format-for-use-with-webcrypto)
```sh
openssl pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in pkcs1.key -out pkcs8.key
```### RSA-OAEP-SHA1
[Openssl has OAEP padding hardcoded using SHA1](https://stackoverflow.com/questions/33532091/rsa-crypto-between-node-js-and-webcrypto)