-
-
Notifications
You must be signed in to change notification settings - Fork 116
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
1 parent
6889c4d
commit 0a6e8e3
Showing
7 changed files
with
55 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,14 @@ | ||
import { generateKeyPairSync } from 'crypto' | ||
import eccrypto from 'eccrypto' | ||
|
||
export const createKeyPair = (): { | ||
publicKey: string | ||
privateKey: string | ||
} => { | ||
const { publicKey, privateKey } = generateKeyPairSync('rsa', { | ||
modulusLength: 2048 | ||
}) | ||
|
||
const publicKeyPEM = publicKey.export({ type: 'pkcs1', format: 'pem' }) | ||
const privateKeyPEM = privateKey.export({ type: 'pkcs1', format: 'pem' }) | ||
const privateKey = eccrypto.generatePrivate() | ||
const publicKey = eccrypto.getPublic(privateKey) | ||
|
||
return { | ||
publicKey: publicKeyPEM.toString(), | ||
privateKey: privateKeyPEM.toString() | ||
publicKey: publicKey.toString('hex'), | ||
privateKey: privateKey.toString('hex') | ||
} | ||
} |
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 |
---|---|---|
@@ -1,12 +1,22 @@ | ||
import { createPrivateKey, privateDecrypt } from 'crypto' | ||
import eccrypto from 'eccrypto' | ||
|
||
export const decrypt = (privateKey: string, data: string): string => { | ||
const privateKeyPEM = createPrivateKey({ | ||
key: privateKey, | ||
format: 'pem' | ||
}) | ||
export const decrypt = async ( | ||
privateKey: string, | ||
data: string | ||
): Promise<string> => { | ||
const parsed = JSON.parse(data) | ||
|
||
const buffer = Buffer.from(data, 'base64') | ||
const decrypted = privateDecrypt(privateKeyPEM, buffer) | ||
return decrypted.toString('utf8') | ||
const eicesData = { | ||
iv: Buffer.from(parsed.iv), | ||
ephemPublicKey: Buffer.from(parsed.ephemPublicKey), | ||
ciphertext: Buffer.from(parsed.ciphertext), | ||
mac: Buffer.from(parsed.mac) | ||
} | ||
|
||
const decrypted = await eccrypto.decrypt( | ||
Buffer.from(privateKey, 'hex'), | ||
eicesData | ||
) | ||
|
||
return decrypted.toString() | ||
} |
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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import { createPublicKey, publicEncrypt } from 'crypto' | ||
import eccrypto from 'eccrypto' | ||
|
||
export const encrypt = (publicKey: string, data: string): string => { | ||
const publicKeyPEM = createPublicKey({ | ||
key: publicKey, | ||
format: 'pem' | ||
}) | ||
export const encrypt = async ( | ||
publicKey: string, | ||
data: string | ||
): Promise<string> => { | ||
const encrypted = await eccrypto.encrypt( | ||
Buffer.from(publicKey, 'hex'), | ||
Buffer.from(data) | ||
) | ||
|
||
const messageBuffer = Buffer.from(data) | ||
const encrypted = publicEncrypt(publicKeyPEM, messageBuffer) | ||
return encrypted.toString('base64') | ||
return JSON.stringify(encrypted) | ||
} |
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
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