rsa-oaep-encryption
Version:
Pure JavaScript implementation of encryption using the RSA-OAEP algorithm without relying on the Web Crypto API.
83 lines (69 loc) • 3.2 kB
Markdown
//img.shields.io/npm/v/rsa-oaep-encryption.svg)](https://npmjs.org/package/rsa-oaep-encryption)
[](https://npmjs.org/package/rsa-oaep-encryption)
[](https://jsr.io/@happy-js/rsa-oaep-encryption)
[](https://jsr.io/@happy-js/rsa-oaep-encryption/score)
[](https://github.com/jiangjie/rsa-oaep-encryption/actions/workflows/test.yml)
[](https://codecov.io/gh/JiangJie/rsa-oaep-encryption)
---
The purpose of this project is to provide a pure JavaScript implementation of `RSA` encryption in an environment that does not support the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API).
Thanks to [node-forge](https://github.com/digitalbazaar/forge), this project was forked from it and the following modifications and changes were made:
* Rewrite using `TypeScript`.
* Delete all `nodejs-only` codes.
* Only the `encryption` function using the `RSA-OAEP` algorithm is retained, and all other codes are deleted. It means that `PKCS1-v1_5` is not supported.
* The hash algorithm only supports `SHA1` `SHA-256` `SHA-384` `SHA-512` to be consistent with the `Web Crypto API`.
* The return value of the `encrypt` method is changed to `ArrayBuffer` to be consistent with the `Web Crypto API`.
* The code is almost `100%` covered and `tree-shake` friendly.
```sh
pnpm add rsa-oaep-encryption
yarn add rsa-oaep-encryption
npm install --save rsa-oaep-encryption
jsr add @happy-js/rsa-oaep-encryption
deno add @happy-js/rsa-oaep-encryption
bunx jsr add @happy-js/rsa-oaep-encryption
```
```ts
import { importPublicKey, sha256 } from 'rsa-oaep-encryption';
const PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
xxxxx
-----END PUBLIC KEY-----`;
const encryptedData = importPublicKey(PUBLIC_KEY).encrypt('some data', sha256.create()); // or sha1 sha384 sha512
```
In an environment that supports the `Web Crypto API`, this is equivalent to the following code:
```ts
const keyData = base64ToBuffer(PUBLIC_KEY.replace(/(-----(BEGIN|END) PUBLIC KEY-----|\s)/g, '')); // base64ToBuffer needs to be implemented
const publicKey = await crypto.subtle.importKey(
'spki',
keyData,
{
name: 'RSA-OAEP',
hash: 'SHA-256',
},
false,
[
'encrypt',
]
);
const encryptedData = await crypto.subtle.encrypt(
{
name: 'RSA-OAEP',
},
publicKey,
new TextEncoder().encode('some data')
);
```
Of course, you can also use the exported `SHA` methods to perform SHA calculations separately in an environment that does not support the `Web Crypto API`.
Like this:
```ts
import { sha1 } from 'rsa-oaep-encryption';
const sha1Str = sha1.create().update('some data').digest().toHex();
```
[![NPM version](https: