-
Notifications
You must be signed in to change notification settings - Fork 16
RSA
Visal .In edited this page Aug 20, 2020
·
14 revisions
import { RSA } from "https://deno.land/x/god_crypto/rsa.ts";
- Usage
- Reference
- Import key with
parseKey
- constructor
- encrypt
- decrypt
- sign
- unsign
- Import key with
- Examples
- Sign and Verify with RS256 for JWT
import { RSA } from "https://deno.land/x/god_crypto/rsa.ts";
const publicKey = RSA.parseKey(Deno.readTextFileSync("./public.pem"));
const cipher = await new RSA(publicKey).encrypt("Hello World");
console.log(ciper.base64());
const privateKey = RSA.parseKey(Deno.readTextFileSync("./private.pem"));
const plain = await new RSA(privateKey).decrypt(cipher);
console.log(plain.toString());
RSA.parseKey(key: string);
Example:
const pem = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnzyis1ZjfNB0bBgKFMSv
vkTtwlvBsaJq7S5wA+kzeVOVpVWwkWdVha4s38XM/pa/yr47av7+z3VTmvDRyAHc
aT92whREFpLv9cj5lTeJSibyr/Mrm/YtjCZVWgaOYIhwrXwKLqPr/11inWsAkfIy
tvHWTxZYEcXLgAXFuUuaS3uF9gEiNQwzGTU1v0FqkqTBr4B8nW3HCN47XUu0t8Y0
e+lf4s4OxQawWD79J9/5d3Ry0vbV3Am1FtGJiJvOwRsIfVChDpYStTcHTCMqtvWb
V6L11BWkpzGXSW4Hv43qa+GSYOD2QU68Mb59oSk2OB+BtOLpJofmbGEGgvmwyCI9
MwIDAQAB
-----END PUBLIC KEY-----`;
const publicKey = RSA.parseKey(pem);
new RSA(key)
Take either public or private key generated by RSA.parseKey
.
const rsa = new RSA(key);
await rsa.encrypt(message, options);
Parameters | Default | Description |
---|---|---|
message |
Message to encrypt. Take Uint8Array or string
|
|
options |
||
>> options.padding |
oaep |
Padding. Support two padding: pkcs1 and oaep
|
>> options.hash |
sha256 |
Hash algorithm used in mask generation function. Support sha1 and sha256 Applied only oaep padding mode |
Example
const rsa = new RSA(publicKey);
await rsa.encrypt("Hello World");
await rsa.encrypt("Hello World", { hash: "sha1" });
await rsa.encrypt("Hello World", { padding: "pkcs1" });
(await rsa.encrypt("Hello World")).hex();
(await rsa.encrypt("Hello World")).base64();
const rsa = new RSA(key);
await rsa.encrypt(cipher, options);
Parameters | Default | Description |
---|---|---|
cipher |
Uint8Array Cipher text to decrypt |
|
options |
||
>> options.padding |
oaep |
Padding. Support two padding: pkcs1 and oaep
|
>> options.hash |
sha256 |
Hash algorithm used in mask generation function. Support sha1 and sha256 Applied only oaep padding mode |
Example
const cipher = await new RSA(publicKey).encrypt("Hello World");
const plain = await new RSA(privateKey).decrypt(cipher);
import { RSA } from "https://deno.land/x/god_crypto/rsa.ts";
import { encode} from "https://deno.land/x/god_crypto/encode.ts";
const jwt =
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiw" +
"iYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.POstGetfAytaZS82wHcjoTyoqhMyxXiWdR7Nn7A29DNSl0E" +
"iXLdwJ6xC6AfgZWF1bOsS_TuYI3OG85AmiExREkrS6tDfTQ2B3WXlrr-wp5AokiRbz3_oB4OxG-W9KcEEbDRcZc0" +
"nH3L7LzYptiy1PtAylQGxHTWZXtGz4ht0bAecBgmpdgXMguEIcoqPJ1n3pIWk_dUZegpqx0Lka21H6XxUTxiy8Oc" +
"aarA8zdnPUnV6AmNP3ecFawIFYdvJB_cm-GvpCSbr8G8y_Mllj8f4x9nBH8pQux89_6gUY618iYv7tuPWBFfEbLx" +
"tF2pZS6YC1aSfLQxeNe8djT9YjpvRZA";
const publicKey = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnzyis1ZjfNB0bBgKFMSv
vkTtwlvBsaJq7S5wA+kzeVOVpVWwkWdVha4s38XM/pa/yr47av7+z3VTmvDRyAHc
aT92whREFpLv9cj5lTeJSibyr/Mrm/YtjCZVWgaOYIhwrXwKLqPr/11inWsAkfIy
tvHWTxZYEcXLgAXFuUuaS3uF9gEiNQwzGTU1v0FqkqTBr4B8nW3HCN47XUu0t8Y0
e+lf4s4OxQawWD79J9/5d3Ry0vbV3Am1FtGJiJvOwRsIfVChDpYStTcHTCMqtvWb
V6L11BWkpzGXSW4Hv43qa+GSYOD2QU68Mb59oSk2OB+BtOLpJofmbGEGgvmwyCI9
MwIDAQAB
-----END PUBLIC KEY-----`;
const [header, payload, signature] = jwt.split(".");
const key = RSA.parseKey(publicKey);
const rsa = new RSA(key);
console.log(await rsa.verify(
encode.base64url(signature),
header + "." + payload,
{ algorithm: "rsassa-pkcs1-v1_5", hash: "sha256" },
));