{"id":205695,"date":"2025-06-12T11:33:23","date_gmt":"2025-06-12T03:33:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205695\/"},"modified":"2025-06-12T11:33:23","modified_gmt":"2025-06-12T03:33:23","slug":"%e5%9c%a8-node-js-%e9%a1%b9%e7%9b%ae%e4%b8%ad%e4%bd%bf%e7%94%a8-golang-%e5%87%bd%e6%95%b0%e7%9a%84%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205695\/","title":{"rendered":"\u5728 Node.js \u9879\u76ee\u4e2d\u4f7f\u7528 Golang \u51fd\u6570\u7684\u6280\u5de7"},"content":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u5728 Node.js \u9879\u76ee\u4e2d\u4f7f\u7528 GOLANG \u51fd\u6570\u7684\u6280\u5de7\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~,\u5728 Node.js \u9879\u76ee\u4e2d\u96c6\u6210 GOLANG \u51fd\u6570\u53ef\u4ee5\u589e\u5f3a\u6027\u80fd\u3002\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5 GOLANG\u521b\u5efa GOLANG \u51fd\u6570\u4f7f\u7528 ffi-napi \u5728 Node.js \u4e2d\u8c03\u7528\u51fd\u6570\u5b9e\u6218\u6848\u4f8b\uff1a\u4f7f\u7528 GOLANG \u51fd\u6570\u4f18\u5316 Node.js \u4e2d\u7684 RSA \u52a0\u5bc6\uff0c\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729945296671cded017092.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u5982\u4f55\u5728 Node.js \u9879\u76ee\u4e2d\u4f7f\u7528 GOLANG \u51fd\u6570<\/strong>,<br \/>\n<strong>\u7b80\u4ecb<\/strong>,\u5728 Node.js \u9879\u76ee\u4e2d\u96c6\u6210 GOLANG \u51fd\u6570\u53ef\u4ee5\u5e26\u6765\u663e\u8457\u7684\u6027\u80fd\u4f18\u52bf\u3002\u672c\u6587\u5c06\u63d0\u4f9b\u4e00\u4e2a\u5206\u6b65\u6307\u5357\uff0c\u4ecb\u7ecd\u5982\u4f55\u5728 Node.js \u9879\u76ee\u4e2d\u4f7f\u7528 GOLANG \u51fd\u6570\uff0c\u5e76\u9644\u6709\u5b9e\u6218\u6848\u4f8b\u3002,<br \/>\n<strong>\u6b65\u9aa4<\/strong>,<br \/>\n<strong>1. \u5b89\u88c5 GOLANG<\/strong>,\u5728\u673a\u5668\u4e0a\u5b89\u88c5 GOLANG\uff0c\u5e76\u786e\u4fdd\u5b83\u5df2\u6dfb\u52a0\u5230\u60a8\u7684 PATH \u73af\u5883\u53d8\u91cf\u4e2d\u3002,<br \/>\n<strong>2. \u521b\u5efa GOLANG \u51fd\u6570<\/strong>,\u7528 GOLANG \u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u4f8b\u5982\uff1a,<br \/>\n<strong>3. \u4ece Node.js \u8c03\u7528 GOLANG \u51fd\u6570<\/strong>,\u4f7f\u7528<br \/>\n<code>ffi-napi<\/code>\uff0c\u4e00\u4e2a\u7528\u4e8e\u5728 Node.js \u4e2d\u8c03\u7528\u672c\u673a C\/C++ \u548c Rust \u51fd\u6570\u7684\u5e93\uff0c\u53ef\u4ee5\u4ece Node.js \u8c03\u7528 GOLANG \u51fd\u6570\u3002,\u5b89\u88c5<br \/>\n<code>ffi-napi<\/code>\uff1a,\u5e76\u5bfc\u5165\u5b83\uff1a,\u521d\u59cb\u5316 GOLANG \u51fd\u6570\uff1a,\u8c03\u7528 GOLANG \u51fd\u6570\uff1a,<br \/>\n<strong>\u5b9e\u6218\u6848\u4f8b<\/strong>,\u4e0b\u9762\u662f\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728 Node.js \u9879\u76ee\u4e2d\u4f7f\u7528 GOLANG \u51fd\u6570\u6765\u4f18\u5316 RSA \u52a0\u5bc6\uff1a,<br \/>\n<strong>Node.js \u4ee3\u7801\uff1a<\/strong>,<br \/>\n<strong>GOLANG \u4ee3\u7801\uff1a<\/strong>,\u8fd9\u5c06\u5927\u5927\u63d0\u9ad8 Node.js \u9879\u76ee\u4e2d\u7684 RSA \u52a0\u5bc6\u6027\u80fd\u3002,\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5728 Node.js \u9879\u76ee\u4e2d\u4f7f\u7528 GOLANG \u51fd\u6570\u7684\u6280\u5de7\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u5427\uff01,\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u5728 Node.js \u9879\u76ee\u4e2d\u4f7f\u7528 GOLANG \u51fd\u6570\u7684\u6280\u5de7\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5728 Node.js \u9879\u76ee\u4e2d\u4f7f\u7528 GOLANG \u51fd\u6570\u7684\u6280\u5de7<\/span><\/p>\n<h1>\u5728 Node.js \u9879\u76ee\u4e2d\u4f7f\u7528 GOLANG \u51fd\u6570\u7684\u6280\u5de7<\/h1>\n<p><span>2024-10-26 20:21:51<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u5728 Node.js \u9879\u76ee\u4e2d\u4f7f\u7528 GOLANG \u51fd\u6570\u7684\u6280\u5de7\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p>\u5728 Node.js \u9879\u76ee\u4e2d\u96c6\u6210 GOLANG \u51fd\u6570\u53ef\u4ee5\u589e\u5f3a\u6027\u80fd\u3002\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5 GOLANG\u521b\u5efa GOLANG \u51fd\u6570\u4f7f\u7528 ffi-napi \u5728 Node.js \u4e2d\u8c03\u7528\u51fd\u6570\u5b9e\u6218\u6848\u4f8b\uff1a\u4f7f\u7528 GOLANG \u51fd\u6570\u4f18\u5316 Node.js \u4e2d\u7684 RSA \u52a0\u5bc6\uff0c\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729945296671cded017092.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 Node.js \u9879\u76ee\u4e2d\u4f7f\u7528 GOLANG \u51fd\u6570<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u5728 Node.js \u9879\u76ee\u4e2d\u96c6\u6210 GOLANG \u51fd\u6570\u53ef\u4ee5\u5e26\u6765\u663e\u8457\u7684\u6027\u80fd\u4f18\u52bf\u3002\u672c\u6587\u5c06\u63d0\u4f9b\u4e00\u4e2a\u5206\u6b65\u6307\u5357\uff0c\u4ecb\u7ecd\u5982\u4f55\u5728 Node.js \u9879\u76ee\u4e2d\u4f7f\u7528 GOLANG \u51fd\u6570\uff0c\u5e76\u9644\u6709\u5b9e\u6218\u6848\u4f8b\u3002<\/p>\n<p><strong>\u6b65\u9aa4<\/strong><\/p>\n<p><strong>1. \u5b89\u88c5 GOLANG<\/strong><\/p>\n<p>\u5728\u673a\u5668\u4e0a\u5b89\u88c5 GOLANG\uff0c\u5e76\u786e\u4fdd\u5b83\u5df2\u6dfb\u52a0\u5230\u60a8\u7684 PATH \u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<p><strong>2. \u521b\u5efa GOLANG \u51fd\u6570<\/strong><\/p>\n<p>\u7528 GOLANG \u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n)\n\n\/\/ GOLANG \u51fd\u6570\nfunc Add(a, b int) int {\n    return a + b\n}\n\nfunc main() {\n    fmt.Println(Add(1, 2)) \/\/ \u8f93\u51fa\uff1a3\n}<\/pre>\n<p><strong>3. \u4ece Node.js \u8c03\u7528 GOLANG \u51fd\u6570<\/strong><\/p>\n<p>\u4f7f\u7528 <code>ffi-napi<\/code>\uff0c\u4e00\u4e2a\u7528\u4e8e\u5728 Node.js \u4e2d\u8c03\u7528\u672c\u673a C\/C++ \u548c Rust \u51fd\u6570\u7684\u5e93\uff0c\u53ef\u4ee5\u4ece Node.js \u8c03\u7528 GOLANG \u51fd\u6570\u3002<\/p>\n<p>\u5b89\u88c5 <code>ffi-napi<\/code>\uff1a<\/p>\n<pre>npm install ffi-napi<\/pre>\n<p>\u5e76\u5bfc\u5165\u5b83\uff1a<\/p>\n<pre>const ffi = require('ffi-napi');<\/pre>\n<p>\u521d\u59cb\u5316 GOLANG \u51fd\u6570\uff1a<\/p>\n<pre>const add = ffi.ForeignFunction(ffi.types.int, [ffi.types.int, ffi.types.int], 'Add');<\/pre>\n<p>\u8c03\u7528 GOLANG \u51fd\u6570\uff1a<\/p>\n<pre>const result = add(1, 2); \/\/ \u8f93\u51fa\uff1a3<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728 Node.js \u9879\u76ee\u4e2d\u4f7f\u7528 GOLANG \u51fd\u6570\u6765\u4f18\u5316 RSA \u52a0\u5bc6\uff1a<\/p>\n<p><strong>Node.js \u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>const crypto = require('crypto');\nconst ffi = require('ffi-napi');\n\nconst rsaSign = ffi.ForeignFunction(ffi.types.CString, [ffi.types.CString, ffi.types.int], 'RSASign');\n\ncrypto.publicEncrypt({}, Buffer.from('Message'), (err, cipher) =&gt; {\n    if (err) throw err;\n    const signature = rsaSign(cipher, cipher.length);\n    \/\/ ... \u5176\u4ed6\u64cd\u4f5c\n});<\/pre>\n<p><strong>GOLANG \u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>package main\n\nimport (\n    \"crypto\/rand\"\n    \"crypto\/rsa\"\n)\n\n\/\/ GOLANG \u51fd\u6570\nfunc RSASign(data []byte, n int) []byte {\n    privateKey, _ := rsa.GenerateKey(rand.Reader, n)\n    signature, _ := rsa.SignPKCS1v15(rand.Reader, privateKey, rsa.HashSHA256, data)\n    return signature\n}<\/pre>\n<p>\u8fd9\u5c06\u5927\u5927\u63d0\u9ad8 Node.js \u9879\u76ee\u4e2d\u7684 RSA \u52a0\u5bc6\u6027\u80fd\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5728 Node.js \u9879\u76ee\u4e2d\u4f7f\u7528 GOLANG \u51fd\u6570\u7684\u6280\u5de7\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u9ad8\u9636\u51fd\u6570\u5728 Java \u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u5e94\u7528\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u67e5\u8be2\u7279\u5b9a\u65e5\u671f\u5185\u540c\u65f6\u62e5\u6709\u591a\u79cd\u5546\u54c1\u7684\u5546\u5e97\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205695","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205695"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205695\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}