Forge TLS 协议的本地实现

开源网站开源项目 24-05-26 13:57:14

Forge 是一个 TLS 协议的本地实现,一个实用的加密程序以及一组利用多网络资源开发 Web 应用的工具。

Transports

TLS:提供本地 JavaScript 客户端和服务器端 TLS 实现。

例如:

//createTLSclientvarclient=forge.tls.createConnection({server:false,caStore:/*ArrayofPEM-formattedcertsoraCAstoreobject*/,sessionCache:{},//supportedciphersuitesinorderofpreferencecipherSuites:[forge.tls.CipherSuites.TLS_RSA_WITH_AES_128_CBC_SHA,forge.tls.CipherSuites.TLS_RSA_WITH_AES_256_CBC_SHA],virtualHost:'example.com',verify:function(connection,verified,depth,certs){if(depth===0){varcn=certs[0].subject.getField('CN').value;if(cn!=='example.com'){verified={alert:forge.tls.Alert.Description.bad_certificate,message:'Certificatecommonnamedoesnotmatchhostname.'};}}returnverified;},connected:function(connection){console.log('connected');//sendmessagetoserverconnection.prepare(forge.util.encodeUtf8('Hiserver!'));/*NOTE:experimental,startheartbeatretransmissiontimermyHeartbeatTimer=setInterval(function(){connection.prepareHeartbeatRequest(forge.util.createBuffer('1234'));},5*60*1000);*/},/*provideaclient-sidecertifyouwantgetCertificate:function(connection,hint){returnmyClientCertificate;},/*theprivatekeyfortheclient-sidecertifprovided*/getPrivateKey:function(connection,cert){returnmyClientPrivateKey;},tlsDataReady:function(connection){//TLSdata(encrypted)isreadytobesenttotheserversendToServerSomehow(connection.tlsData.getBytes());//ifyouwerecommunicatingwiththeserverbelow,you'ddo://server.process(connection.tlsData.getBytes());},dataReady:function(connection){//cleardatafromtheserverisreadyconsole.log('theserversent:'+forge.util.decodeUtf8(connection.data.getBytes()));//closeconnectionconnection.close();},/*NOTE:experimentalheartbeatReceived:function(connection,payload){//restartretransmissiontimer,lookatpayloadclearInterval(myHeartbeatTimer);myHeartbeatTimer=setInterval(function(){connection.prepareHeartbeatRequest(forge.util.createBuffer('1234'));},5*60*1000);payload.getBytes();},*/closed:function(connection){console.log('disconnected');},error:function(connection,error){console.log('uhoh',error);}});//startthehandshakeprocessclient.handshake();//whenencryptedTLSdataisreceivedfromtheserver,processitclient.process(encryptedBytesFromServer);//createTLSservervarserver=forge.tls.createConnection({server:true,caStore:/*ArrayofPEM-formattedcertsoraCAstoreobject*/,sessionCache:{},//supportedciphersuitesinorderofpreferencecipherSuites:[forge.tls.CipherSuites.TLS_RSA_WITH_AES_128_CBC_SHA,forge.tls.CipherSuites.TLS_RSA_WITH_AES_256_CBC_SHA],//requireaclient-sidecertificateifyouwantverifyClient:true,verify:function(connection,verified,depth,certs){if(depth===0){varcn=certs[0].subject.getField('CN').value;if(cn!=='the-client'){verified={alert:forge.tls.Alert.Description.bad_certificate,message:'Certificatecommonnamedoesnotmatchexpectedclient.'};}}returnverified;},connected:function(connection){console.log('connected');//sendmessagetoclientconnection.prepare(forge.util.encodeUtf8('Hiclient!'));/*NOTE:experimental,startheartbeatretransmissiontimermyHeartbeatTimer=setInterval(function(){connection.prepareHeartbeatRequest(forge.util.createBuffer('1234'));},5*60*1000);*/},getCertificate:function(connection,hint){returnmyServerCertificate;},getPrivateKey:function(connection,cert){returnmyServerPrivateKey;},tlsDataReady:function(connection){//TLSdata(encrypted)isreadytobesenttotheclientsendToClientSomehow(connection.tlsData.getBytes());//ifyouwerecommunicatingwiththeclientaboveyou'ddo://client.process(connection.tlsData.getBytes());},dataReady:function(connection){//cleardatafromtheclientisreadyconsole.log('theclientsent:'+forge.util.decodeUtf8(connection.data.getBytes()));//closeconnectionconnection.close();},/*NOTE:experimentalheartbeatReceived:function(connection,payload){//restartretransmissiontimer,lookatpayloadclearInterval(myHeartbeatTimer);myHeartbeatTimer=setInterval(function(){connection.prepareHeartbeatRequest(forge.util.createBuffer('1234'));},5*60*1000);payload.getBytes();},*/closed:function(connection){console.log('disconnected');},error:function(connection,error){console.log('uhoh',error);}});//whenencryptedTLSdataisreceivedfromtheclient,processitserver.process(encryptedBytesFromClient);
[Forge TLS 协议的本地实现]相关推荐

Dale 系统编程语言

Dale 是一个系统编程语言,使用 S- 表达式作为语法,支持语义宏。基本语言类似 C,提供如下额外特性: 局……...

SWT 本地化GUI组件库

SWT 本地化GUI组件库

SWT-"Standard Widget Toolkit",它是一个Java平台下开放源码的Native GUI组件库,也是Eclipse平台的UI组……...

Jmr 生成代码的 Eclipse 插件

Jmr 生成代码的 Eclipse 插件

Jmr是一款基于模型驱动开发,根据自己的项目和框架编写模板,生成代码的Eclipse插件。 模板:模板类似Js……...

hoe Java 基础操作库

hoe 是一个 Java 基础操作库,包括 String,number,random 等常规操作,几乎包含了大部分工作中用到的常……...

FastAPI Python 高性能 Web 框架

FastAPI 是一个高性能 Web 框架,用于构建 API。 主要特性: 快速:非常高的性能,与 NodeJS 和 Go 相当 ……...

今日开源
  1. kele 任务管理系统

    kele 任务管理系统

    自动生成数据库,HTML 表单,以此为基础,扩展各种表单和数据库,数据库字段自动匹配数据的格式及处理。 希望由面向程序员到面向用户,用户直接做做选择题,即能实现想要的功能。 系统说明: 第一步建字段,先设想……

    开源软件 2024-07-02

  2. Thyme 自动追踪使用应用程序的时间

    Thyme 可以自动追踪你使用应用程序的时间。 特性: 简单的命令行界面: 每 30s 记录一次你使用的应用 $ while true; do thyme track -o thyme.json; sleep 30s; done; 在新窗口创建图表以显示……

    开源软件 2024-07-02

  3. Una 尤娜博客系统

    Una 尤娜博客系统

    Una [‘尤娜’] 只是一个项目代号,没有特殊含义。尤娜是站在巨人的肩膀上开发完成的博客系统,旨在为程序员提供一个极简的内容创作管理平台,尤娜100%开放源代码,如果您对她感兴趣,Fork她,并尽情的……

    开源软件 2024-07-02

返回顶部小火箭