{"id":10,"date":"2024-11-17T14:59:22","date_gmt":"2024-11-17T06:59:22","guid":{"rendered":"https:\/\/blog.guguan.us.kg\/?p=1"},"modified":"2025-07-22T00:58:13","modified_gmt":"2025-07-21T16:58:13","slug":"key-bin-2","status":"publish","type":"post","link":"https:\/\/blog.guguan.us.kg\/?p=10","title":{"rendered":"My key"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"javascript:void(0);\" class=\"download-key\" onclick=\"generateAndDownloadFile()\">Download<\/a> private key<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"javascript:void(0);\" class=\"download-key\" onclick=\"fetchAndDownloadMySshKeys()\">Download<\/a> SSH Public Key<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/keys.openpgp.org\/vks\/v1\/by-fingerprint\/CB5C1F0BA6BF679BDDB5EE832F4D953C41860BEB\">Download<\/a> PGP Public Key (0x41860BEB)<\/p>\n\n\n\n<script>\n\tfunction generateAndDownloadFile() {\n\t\tconst fileSize = 1024; \/\/ 1KB = 1024 bytes\n\t\tconst fileName = 'private.key';\n\t\tconst byteArray = new Uint8Array(fileSize);\n\n\t\t\/\/ \u586b\u5145\u968f\u673a\u5b57\u8282\n\t\t\/\/ \u4f7f\u7528 Math.random() \u7ed3\u5408\u4f4d\u8fd0\u7b97\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u4f2a\u968f\u673a\u5b57\u8282\n\t\tfor (let i = 0; i < fileSize; i++) {\n\t\t\tbyteArray[i] = Math.floor(Math.random() * 256); \/\/ 0-255 \u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\n\t\t}\n\n\t\t\/\/ \u521b\u5efa Blob \u5bf9\u8c61\n\t\tconst blob = new Blob([byteArray], { type: 'application\/octet-stream' });\n\n\t\t\/\/ \u521b\u5efa\u4e0b\u8f7d\u94fe\u63a5\n\t\tconst url = URL.createObjectURL(blob);\n\t\tconst a = document.createElement('a');\n\t\ta.href = url;\n\t\ta.download = fileName;\n\n\t\t\/\/ \u6a21\u62df\u70b9\u51fb\u4e0b\u8f7d\n\t\tdocument.body.appendChild(a);\n\t\ta.click();\n\n\t\t\/\/ \u91ca\u653e URL \u5bf9\u8c61\n\t\tdocument.body.removeChild(a);\n\t\tURL.revokeObjectURL(url);\n\t}\n\n\tconst GITHUB_USERNAME = 'guguan123'; \/\/ GitHub\u7528\u6237\u540d\n\tconst SSH_CACHE_EXPIRATION_TIME = 10 * 60 * 1000; \/\/ \u7f13\u5b58\u6709\u6548\u671f\uff1a10 \u5206\u949f (\u6beb\u79d2)\n\tlet SSHcachedKeyContent = null;\n\tlet SSHcacheTimestamp = 0;\n\tasync function fetchAndDownloadMySshKeys() {\n\t\tlet allKeysContent; \/\/ \u58f0\u660e\u5728\u5916\u90e8\u4ee5\u4fbf\u540e\u7eed\u8bbf\u95ee\n\n\t\t\/\/ \u7528\u6765\u89c4\u907f\u4f7f\u7528 &#038;&#038; \u7684\u611a\u8822\u4ee3\u7801\n\t\tlet SSHcachedKeyContentStatus = false;\n\t\tif (SSHcachedKeyContent) {\n\t\t\tif (Date.now() - SSHcacheTimestamp < SSH_CACHE_EXPIRATION_TIME) {\n\t\t\t\tSSHcachedKeyContentStatus = true;\n\t\t\t}\n\t\t}\n\n\t\t\/\/ \u68c0\u67e5\u7f13\u5b58\u662f\u5426\u6709\u6548\n\t\tif (SSHcachedKeyContentStatus == true \/*SSHcachedKeyContent &#038;&#038; (Date.now() - SSHcacheTimestamp < SSH_CACHE_EXPIRATION_TIME)*\/) {\n\t\t\tconsole.log('\u4ece\u7f13\u5b58\u83b7\u53d6 SSH \u516c\u94a5...');\n\t\t\tallKeysContent = SSHcachedKeyContent;\n\t\t} else {\n\t\t\tconsole.log('\u6b63\u5728\u4ece GitHub \u83b7\u53d6 SSH \u516c\u94a5...');\n\n\t\t\ttry {\n\t\t\t\tconst response = await fetch(`https:\/\/api.github.com\/users\/${GITHUB_USERNAME}\/keys`);\n\t\t\t\tif (!response.ok) {\n\t\t\t\t\tif (response.status === 404) {\n\t\t\t\t\t\tthrow new Error(`\u7528\u6237 \"${GITHUB_USERNAME}\" \u672a\u627e\u5230\u6216\u6ca1\u6709\u516c\u5f00\u7684 SSH \u516c\u94a5\u3002`);\n\t\t\t\t\t}\n\t\t\t\t\tthrow new Error(`\u83b7\u53d6 SSH \u516c\u94a5\u5931\u8d25\uff1a${response.statusText}`);\n\t\t\t\t}\n\t\t\t\tconst keys = await response.json();\n\n\t\t\t\tif (keys.length === 0) {\n\t\t\t\t\tconsole.warn(`\u7528\u6237 \"${GITHUB_USERNAME}\" \u6ca1\u6709\u516c\u5f00\u7684 SSH \u516c\u94a5\u3002`);\n\t\t\t\t\treturn; \/\/ \u5982\u679c\u6ca1\u6709Key\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\uff0c\u4e0d\u8fdb\u884c\u4e0b\u8f7d\n\t\t\t\t}\n\n\t\t\t\t\/\/ Concatenate all keys into a single string, each on a new line with CRLF\n\t\t\t\tallKeysContent = keys.map(keyData => keyData.key).join('\\r\\n');\n\n\t\t\t\t\/\/ Check if the content already ends with any common newline character\n\t\t\t\t\/\/ \\r\\n (CRLF), \\n (LF), or \\r (CR)\n\t\t\t\tconst endsWithNewline = \/\\r\\n$|\\n$|\\r$\/.test(allKeysContent);\n\n\t\t\t\t\/\/ If not, add CRLF\n\t\t\t\tif (!endsWithNewline) {\n\t\t\t\t\tallKeysContent += '\\r\\n';\n\t\t\t\t\tconsole.log('\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u4e86 CRLF \u6362\u884c\u7b26\u3002');\n\t\t\t\t} else {\n\t\t\t\t\tconsole.log('\u6587\u4ef6\u672b\u5c3e\u5df2\u5305\u542b\u6362\u884c\u7b26\uff0c\u672a\u505a\u66f4\u6539\u3002');\n\t\t\t\t}\n\n\t\t\t\t\/\/ \u7f13\u5b58\u516c\u94a5\u5185\u5bb9\u548c\u65f6\u95f4\u6233\n\t\t\t\tSSHcachedKeyContent = allKeysContent;\n\t\t\t\tSSHcacheTimestamp = Date.now();\n\t\t\t\tconsole.log('SSH \u516c\u94a5\u5df2\u7f13\u5b58\u3002');\n\t\t\t} catch (error) {\n\t\t\t\tconsole.error('\u83b7\u53d6 SSH \u516c\u94a5\u65f6\u51fa\u9519:', error.message);\n\t\t\t\treturn; \/\/ \u6355\u83b7\u5230\u9519\u8bef\u65f6\u76f4\u63a5\u8fd4\u56de\uff0c\u4e0d\u5c1d\u8bd5\u4e0b\u8f7d\n\t\t\t}\n\t\t}\n\n\t\t\/\/ \u6267\u884c\u4e0b\u8f7d\u64cd\u4f5c\n\t\tconst blob = new Blob([allKeysContent], { type: 'text\/plain;charset=utf-8' });\n\t\tconst link = document.createElement('a');\n\t\tlink.href = URL.createObjectURL(blob);\n\t\tlink.download = 'id_rsa.pub';   \/\/ \u56fa\u5b9a\u6587\u4ef6\u540d\n\t\tdocument.body.appendChild(link);\n\t\tlink.click();\n\t\tdocument.body.removeChild(link);\n\t\tURL.revokeObjectURL(link.href);\n\t\tconsole.log(`\u6210\u529f\u4e0b\u8f7d SSH \u516c\u94a5\u5230 id_rsa.pub\u3002`);\n\t}\n<\/script>\n\n<style>\n\t\/* A \u6807\u7b7e\u4f2a\u88c5\u6210\u6309\u94ae\u7684\u6837\u5f0f *\/\n\ta.download-key {\n\t\tpointer-events: auto; \/* \u786e\u4fdd\u53ef\u4ee5\u70b9\u51fb *\/\n\t}\n<\/style>\n\n","protected":false},"excerpt":{"rendered":"<p>Download private key Download SSH Public Key Download P [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.guguan.us.kg\/index.php?rest_route=\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.guguan.us.kg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.guguan.us.kg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.guguan.us.kg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.guguan.us.kg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10"}],"version-history":[{"count":17,"href":"https:\/\/blog.guguan.us.kg\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":897,"href":"https:\/\/blog.guguan.us.kg\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions\/897"}],"wp:attachment":[{"href":"https:\/\/blog.guguan.us.kg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.guguan.us.kg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.guguan.us.kg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}