8 Star 33 Fork 14

Cliven/gotlcp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 2.92 KB
一键复制 编辑 原始数据 按行查看 历史
Cliven 提交于 2022-08-23 22:50 . 更新:
package main
import (
"fmt"
"gitee.com/Trisia/gotlcp/tlcp"
"github.com/emmansun/gmsm/smx509"
)
// 客户端双向身份认证
func main() {
loadCert()
// 构造根证书列表
pool := smx509.NewCertPool()
pool.AddCert(rootCert)
config := &tlcp.Config{
RootCAs: pool,
Certificates: []tlcp.Certificate{authCertKeypair},
CipherSuites: []uint16{
tlcp.ECDHE_SM4_GCM_SM3, // 最高优先级
tlcp.ECC_SM4_GCM_SM3,
tlcp.ECC_SM4_CBC_SM3, // 最低优先级
},
}
conn, err := tlcp.Dial("tcp", "127.0.0.1:8450", config)
if err != nil {
panic(err)
}
defer conn.Close()
_, err = conn.Write([]byte("Hello Go TLCP!"))
if err != nil {
panic(err)
}
buff := make([]byte, 512)
n, err := conn.Read(buff)
if err != nil {
panic(err)
}
fmt.Printf("<< %s\n", buff[:n])
}
const (
ROOT_CERT_PEM = `-----BEGIN CERTIFICATE-----
MIIB3jCCAYOgAwIBAgIIAs4MAPwpIBcwCgYIKoEcz1UBg3UwQjELMAkGA1UEBhMC
Q04xDzANBgNVBAgMBua1meaxnzEPMA0GA1UEBwwG5p2t5beeMREwDwYDVQQKDAjm
tYvor5VDQTAeFw0yMTEyMjMwODQ4MzNaFw0zMTEyMjMwODQ4MzNaMEIxCzAJBgNV
BAYTAkNOMQ8wDQYDVQQIDAbmtZnmsZ8xDzANBgNVBAcMBuadreW3njERMA8GA1UE
CgwI5rWL6K+VQ0EwWTATBgcqhkjOPQIBBggqgRzPVQGCLQNCAARKs6B5ZBy753Os
ZSeIfv8zScbiiXkLjB+Plw+YWvoesRkqYGe/Mqjr8rrmThq6iEWubYK6ZiQQV54k
Klcva3Hto2MwYTAOBgNVHQ8BAf8EBAMCAQYwD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_KEY_PEM = `-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQg84KPC3TBK6YaNI9w
WUXKWtumd+Ntd1Aid1Wk0CLbRaegCgYIKoEcz1UBgi2hRANCAATOyJ0LJKSf9lHw
J9Bq7pe38CkohX2biKwJScLBTfNO/+bA4VvBndoY3FEgY76kHL0YhEuoPwIQqkU4
OA/nCdeU
-----END PRIVATE KEY-----
`
)
var (
rootCert *smx509.Certificate
authCertKeypair tlcp.Certificate
)
func loadCert() {
var err error
rootCert, err = smx509.ParseCertificatePEM([]byte(ROOT_CERT_PEM))
if err != nil {
panic(err)
}
authCertKeypair, err = tlcp.X509KeyPair([]byte(AUTH_CERT_PEM), []byte(AUTH_KEY_PEM))
if err != nil {
panic(err)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/Trisia/gotlcp.git
git@gitee.com:Trisia/gotlcp.git
Trisia
gotlcp
gotlcp
v1.1.3

搜索帮助

0d507c66 1850385 C8b1a773 1850385