-
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
Showing
8 changed files
with
153 additions
and
6 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
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
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,5 @@ | ||
export default class DecryptionException extends Error { | ||
constructor(error: unknown) { | ||
super(`Failed to decrypt data.\nDetails:\n\t:${error}`); | ||
} | ||
} |
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
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,93 @@ | ||
import { subtle } from "crypto"; | ||
import { ByteArray, bytesToBase64 } from "./types"; | ||
import DecryptionException from "./exceptions/DecryptionException"; | ||
|
||
interface KeyPair { | ||
pub: ByteArray; | ||
secret: ByteArray; | ||
} | ||
|
||
/** | ||
* Generates a pair of RSA keys. | ||
* | ||
* @returns An RSA key pair. | ||
*/ | ||
async function generateRSAKeyPair(): Promise<KeyPair> { | ||
const cryptoKeyPair = await subtle.generateKey( | ||
{ | ||
name: "RSA-OAEP", | ||
modulusLength: 2048, | ||
publicExponent: new Uint8Array([0x01, 0x00, 0x01]), | ||
hash: { name: "SHA-256" } | ||
}, | ||
true, | ||
["encrypt", "decrypt"] | ||
); | ||
|
||
const pub: ByteArray = new Uint8Array(await subtle.exportKey("spki", cryptoKeyPair.publicKey)); | ||
const secret: ByteArray = new Uint8Array( | ||
await subtle.exportKey("pkcs8", cryptoKeyPair.privateKey) | ||
); | ||
|
||
return { pub, secret }; | ||
} | ||
|
||
/** | ||
* Exports the given RSA key as PEM format. | ||
* | ||
* @param key The key to be exported. | ||
* @param keyType The type of the key ("PUBLIC" for public key, "SECRET" for private key). | ||
* @returns The RSA key in PEM format. | ||
*/ | ||
function exportAsPem(key: ByteArray, keyType: "PUBLIC" | "SECRET"): string { | ||
const base64Key = bytesToBase64(new Uint8Array(key)); | ||
return `-----BEGIN ${keyType == "PUBLIC" ? "PUBLIC" : "RSA PRIVATE"} KEY-----\n${base64Key}\n-----END ${keyType == "PUBLIC" ? "PUBLIC" : "RSA PRIVATE"} KEY-----`; | ||
} | ||
|
||
/** | ||
* Encrypts the given data using the provided RSA public key. | ||
* | ||
* @param pub The RSA public key used for encryption. | ||
* @param data The data to be encrypted. | ||
* @returns The encrypted data. | ||
*/ | ||
async function rsaEncrypt(pub: ByteArray, data: ByteArray): Promise<ByteArray> { | ||
const publicKey = await subtle.importKey( | ||
"spki", | ||
pub, | ||
{ name: "RSA-OAEP", hash: "SHA-256" }, | ||
true, | ||
["encrypt"] | ||
); | ||
|
||
const encryptedData = await subtle.encrypt({ name: "RSA-OAEP" }, publicKey, data); | ||
|
||
return new Uint8Array(encryptedData); | ||
} | ||
|
||
/** | ||
* Decrypts the given cipher using the provided RSA private key. | ||
* | ||
* @param secret The RSA private key used for decryption. | ||
* @param cipher The ciphered data to be decrypted. | ||
* @returns The decrypted data. | ||
*/ | ||
async function rsaDecrypt(secret: ByteArray, cipher: ByteArray): Promise<ByteArray> { | ||
try { | ||
const privateKey = await subtle.importKey( | ||
"pkcs8", | ||
secret, | ||
{ name: "RSA-OAEP", hash: "SHA-256" }, | ||
true, | ||
["decrypt"] | ||
); | ||
|
||
const decryptedData = await subtle.decrypt({ name: "RSA-OAEP" }, privateKey, cipher); | ||
|
||
return new Uint8Array(decryptedData); | ||
} catch (error) { | ||
throw new DecryptionException(error); | ||
} | ||
} | ||
|
||
export { generateRSAKeyPair, exportAsPem, rsaEncrypt, rsaDecrypt }; |
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
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,46 @@ | ||
import { exportAsPem, generateRSAKeyPair, rsaDecrypt, rsaEncrypt } from "../src/rsa"; | ||
import { base64ToBytes, ByteArray } from "../src/types"; | ||
import DecryptionException from "../src/exceptions/DecryptionException"; | ||
|
||
describe("RSA", () => { | ||
const pub: ByteArray = base64ToBytes( | ||
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi/7MWgu2nObTHZ6+k//1YsA6CkS7o9FjA1yegUPb6ZTZcTFeeL/L4b/oLZZuWC76pqsobwwxyqr8uWfd5uBerCHqsXuvda30RVgLwDSXDNzXHXhAUbWafeCypMABG6MMLbjcFSmy+15fygZOHYCpmEAM1nePWUADbQTch3YM7ZvzAZEb69IKviP49Lty30KKgUZInYu4nYsqGfhINCR6E5d6su/tVWrVCHn7eoomTh2+TR9DDpRMfKuA4PstRu4wNsTIm2QUESkTlBexEPG+nCQZETe1mXfcu/zIJ+JhdZvIDp8sgDc0YP2KllpNQQAfSvZWi59WnfSrHPrn9YQXhQIDAQAB" | ||
); | ||
const secret: ByteArray = base64ToBytes( | ||
"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCL/sxaC7ac5tMdnr6T//ViwDoKRLuj0WMDXJ6BQ9vplNlxMV54v8vhv+gtlm5YLvqmqyhvDDHKqvy5Z93m4F6sIeqxe691rfRFWAvAN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On0K2YawOHZKCimWBejpLvjRr+X0bfbr7J/ZFHZFpGUxAoGBALNHxUtDQqZLaq+EJ7KTpzE8hXMZSdnpPAxqxM82Wd3q4VAhyXN7DxvYgEt3SZtmMqCneNN+fCt7GACT82vLpI7G62xspDEF2vT7v+Nehs5lul9s/NuGva+yUMcKjhFX6auUBnHrFhwv6+y1ynfH3phTPjcbUNXrN6e3XhO8FKo9AoGAYkG2zNdPJdp8NEr/KX0RNhBna1BdG9010dx3d0Is59hMTWvHObmqbWCTISeccfFuY7tV0XzRaFa5aOl78Fs9+SvDya2OXWaR1SW78xC255+diBMRNee887hCKKKoi5C5uBk7n6PRamrvSnahGWlrPRO6Cl+wYMFcezNrSakCwFA=" | ||
); | ||
const data: ByteArray = base64ToBytes("c2VjdXJpdHk="); | ||
|
||
it("should generate a key pair", async () => { | ||
const keyPair = await generateRSAKeyPair(); | ||
|
||
expect(keyPair.secret.length).toBeGreaterThan(1000); | ||
expect(keyPair.pub.length).toBeGreaterThan(200); | ||
}); | ||
|
||
it("should export keys", () => { | ||
const publicPem = exportAsPem(pub, "PUBLIC"); | ||
const secretPem = exportAsPem(secret, "SECRET"); | ||
|
||
expect(publicPem).toMatch(/^-----BEGIN PUBLIC KEY-----[\s\w/+=]+-----END PUBLIC KEY-----$/); | ||
expect(secretPem).toMatch( | ||
/^-----BEGIN RSA PRIVATE KEY-----[\s\w/+=]+-----END RSA PRIVATE KEY-----$/ | ||
); | ||
}); | ||
|
||
it("should encrypt and decrypt data", async () => { | ||
const cipher: ByteArray = await rsaEncrypt(pub, data); | ||
const decryptedData: ByteArray = await rsaDecrypt(secret, cipher); | ||
|
||
expect(decryptedData).toEqual(data); | ||
}); | ||
|
||
it("should not be able to decrypt data", async () => { | ||
const cipher: ByteArray = await rsaEncrypt(pub, data); | ||
const wrongKeyPair = await generateRSAKeyPair(); | ||
|
||
await expect(async () => await rsaDecrypt(wrongKeyPair.secret, cipher)).rejects.toThrow( | ||
DecryptionException | ||
); | ||
}); | ||
}); |