Created
August 4, 2020 15:01
-
-
Save hmmhmmhm/25e7edeeeaa74a157706e0a9a2b9d112 to your computer and use it in GitHub Desktop.
RSA 비대칭키 암호화 / 복호화 / 서명 / 검증 예시 (자바스크립트 겸 타입스크립트)
This file contains hidden or 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
| import forge from 'node-forge' | |
| type encodingType = "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" | undefined | |
| export const generateKeyPair = ( | |
| options: forge.pki.rsa.GenerateKeyPairOptions = { bits: 2048, workers: 2, e: 0x10001 } | |
| ): Promise<{ | |
| publicKey: string | |
| privateKey: string | |
| }> => { | |
| return new Promise((resolve) => { | |
| forge.pki.rsa.generateKeyPair(options, (err, keypair) => { | |
| resolve({ | |
| publicKey: forge.pki.publicKeyToPem(keypair.publicKey), | |
| privateKey: forge.pki.privateKeyToPem(keypair.privateKey), | |
| }) | |
| }) | |
| }) | |
| } | |
| export const encrypt = ( | |
| message: string, | |
| publicKey: string, | |
| format: encodingType = 'base64' | |
| ) => { | |
| return Buffer.from(forge.pki.publicKeyFromPem(publicKey).encrypt(forge.util.encodeUtf8(message))).toString(format) | |
| } | |
| export const decrypt = ( | |
| encrypted: string, | |
| privateKey: string, | |
| formmat: encodingType = 'base64' | |
| ) => { | |
| return forge.pki.privateKeyFromPem(privateKey).decrypt(Buffer.from(encrypted, formmat).toString()) | |
| } | |
| export const sign = ( | |
| message: string, | |
| privateKey: string, | |
| format: encodingType = 'base64' | |
| ) => { | |
| const md = forge.md.sha1.create() | |
| md.update(forge.util.encodeUtf8(message), 'utf8') | |
| return Buffer.from(forge.pki.privateKeyFromPem(privateKey).sign(md)).toString(format) | |
| } | |
| export const verify = ( | |
| signature: string, | |
| message: string, | |
| publicKey: string, | |
| format: encodingType = 'base64' | |
| ) => { | |
| const md = forge.md.sha1.create() | |
| md.update(forge.util.encodeUtf8(message), 'utf8') | |
| return forge.pki.publicKeyFromPem(publicKey).verify(md.digest().bytes(), Buffer.from(signature, format).toString()) | |
| } | |
| (async () => { | |
| const { privateKey, publicKey } = await generateKeyPair() | |
| console.log(`\nprivateKey: ${privateKey}`) | |
| console.log(`\npublicKey: ${publicKey}`) | |
| const encrypted = encrypt('test', publicKey) | |
| const decrypted = decrypt(encrypted, privateKey) | |
| console.log(`\nencrypted: ${encrypted}`) | |
| console.log(`\ndecrypted: ${decrypted}`) | |
| const signed = sign('my public sign', privateKey) | |
| const verified = verify(signed, 'my public sign', publicKey) | |
| console.log(`\nsigned: ${signed}`) | |
| console.log(`\nverified: ${verified}`) | |
| })() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment