Path: blob/master/api-rsa-generator/src/App.js
518 views
import './App.css';1import { Button, Select, Space, Card, Row, Col } from 'antd';2import TextArea from 'antd/es/input/TextArea';3import { useState } from "react";4import "crypto";56async function generateKeysWithBits (bits) {7const result = await crypto.subtle.generateKey(8{9name: "RSASSA-PKCS1-v1_5",10modulusLength: parseInt(bits),11publicExponent: new Uint8Array([1, 0, 1]),12hash: "SHA-256"13},14true,15["sign", "verify"]16).then(function(res){17return res18});1920let priv = await crypto.subtle.exportKey("pkcs8", result.privateKey)21priv = Buffer.from(priv).toString('base64')22priv = priv.match(/.{1,64}/g).join('\n')23let privStr = "-----BEGIN PRIVATE KEY-----\n" + priv + "\n-----END PRIVATE KEY-----"2425let pub = await crypto.subtle.exportKey("spki", result.publicKey)26pub = Buffer.from(pub).toString('base64')27pub = pub.match(/.{1,64}/g).join('\n')28let pubStr = "-----BEGIN PUBLIC KEY-----\n" + pub + "\n-----END PUBLIC KEY-----"29return {"priv": privStr, "pub": pubStr}30}3132function App() {33var selectVal = "2048"34const [loading, setLoading] = useState(Boolean)35const [privVal, setPrivVal] = useState("")36const [pubVal, setPubVal] = useState("")3738const generateKeys = async () => {39setLoading(true)40setPrivVal("")41setPubVal("")42const res = await generateKeysWithBits(parseInt(selectVal))43setPrivVal(res.priv)44setPubVal(res.pub)45setLoading(false)46}47const copyPriv = async () => {48navigator.clipboard.writeText(privVal)49}50const copyPub = async () => {51navigator.clipboard.writeText(pubVal)52}5354return (55<div className="App">56<Space direction="vertical" style={{ width:"100%" }}>57<Row>58<h2>Generate Keys</h2>59</Row>60<Row>61<p>You can generate an RSA PKCS#8 private key and public key of 2048-bit(recommend) or 4096-bit.</p>62</Row>63<Row>64<Select65defaultValue={{value: '2048', label: '2048-bit'}}66onChange={(value) => {selectVal = value}}67options={[68{69value: '2048',70label: '2048-bit'71},72{73value: '4096',74label: '4096-bit'75},76]}77/>78</Row>79<Row>80<Button type="primary" onClick={generateKeys} loading={loading}>Generate Keys</Button>81</Row>8283<Row style={{width:"100%"}}>84<Col span={11}>85<Card title="Private Key" size='middle'>86<TextArea autoSize={{ minRows: 8, maxRows: 8 }} value={privVal} spellCheck="false"></TextArea>87<Button type="primary" style={{ marginTop: "2vh" }} onClick={copyPriv} block>Copy</Button>88</Card>89</Col>90<Col span={11} offset={1}>91<Card title="Public Key" size='middle'>92<TextArea autoSize={{ minRows: 8, maxRows: 8 }} value={pubVal} spellCheck="false"></TextArea>93<Button type="primary" style={{ marginTop: "2vh" }} onClick={copyPub} block>Copy</Button>94</Card>95</Col>96</Row>97</Space>98</div>99);100}101102export default App;103104105