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