update doc and README

master
fangdingjun 6 years ago
parent a2891d6cbc
commit 5093a0837b

@ -17,18 +17,18 @@ TLS client example
InsecureSkipVerify: true, InsecureSkipVerify: true,
}) })
if err != nil { if err != nil {
t.Fatal("gnutls dial ", err) log.Fatal("gnutls dial ", err)
} }
defer c.Close() defer c.Close()
data := "hello, world" data := "hello, world"
if _, err = c.Write([]byte(data)); err != nil { if _, err = c.Write([]byte(data)); err != nil {
t.Fatal("gnutls write ", err) log.Fatal("gnutls write ", err)
} }
buf := make([]byte, 100) buf := make([]byte, 100)
n, err := c.Read(buf) n, err := c.Read(buf)
if err != nil { if err != nil {
t.Fatal("gnutls read ", err) log.Fatal("gnutls read ", err)
} }
TLS Server example TLS Server example
@ -76,33 +76,33 @@ AES encrypt/decrypt example
iv := []byte("abcdefg123456789") iv := []byte("abcdefg123456789")
c, err := gnutls.NewCipher(gnutls.GNUTLS_CIPHER_AES_128_CBC, key, iv) c, err := gnutls.NewCipher(gnutls.GNUTLS_CIPHER_AES_128_CBC, key, iv)
if err != nil { if err != nil {
t.Fatal(err) log.Fatal(err)
} }
defer c.Close() defer c.Close()
c1, err := gnutls.NewCipher(gnutls.GNUTLS_CIPHER_AES_128_CBC, key, iv) c1, err := gnutls.NewCipher(gnutls.GNUTLS_CIPHER_AES_128_CBC, key, iv)
if err != nil { if err != nil {
t.Fatal(err) log.Fatal(err)
} }
defer c1.Close() defer c1.Close()
data := []byte("1234012121212121") data := []byte("1234012121212121")
if c == nil { if c == nil {
t.Fatal("new ciphoer failed") log.Fatal("new ciphoer failed")
} }
// encrypt // encrypt
dst := make([]byte, len(data)) dst := make([]byte, len(data))
err := c.Encrypt(dst, data) err := c.Encrypt(dst, data)
if err != nil { if err != nil {
t.Fatal("encrypt failed", err) log.Fatal("encrypt failed", err)
} }
// decrypt // decrypt
data1 := make([]byte, len(data)) data1 := make([]byte, len(data))
err := c1.Decrypt(data1, cdata) err := c1.Decrypt(data1, cdata)
if err != nil { if err != nil {
t.Fatal("decrypt failed", err) log.Fatal("decrypt failed", err)
} }

@ -9,18 +9,18 @@ TLS client example:
addr := "127.0.0.1:9443" addr := "127.0.0.1:9443"
c, err := gnutls.Dial("tcp", addr, &gnutls.Config{ServerName: "localhost",InsecureSkipVerify: true}) c, err := gnutls.Dial("tcp", addr, &gnutls.Config{ServerName: "localhost",InsecureSkipVerify: true})
if err != nil { if err != nil {
t.Fatal("gnutls dial ", err) log.Fatal("gnutls dial ", err)
} }
defer c.Close() defer c.Close()
data := "hello, world" data := "hello, world"
if _, err = c.Write([]byte(data)); err != nil { if _, err = c.Write([]byte(data)); err != nil {
t.Fatal("gnutls write ", err) log.Fatal("gnutls write ", err)
} }
buf := make([]byte, 100) buf := make([]byte, 100)
n, err := c.Read(buf) n, err := c.Read(buf)
if err != nil { if err != nil {
t.Fatal("gnutls read ", err) log.Fatal("gnutls read ", err)
} }
TLS Server example: TLS Server example:
@ -65,33 +65,33 @@ AES encrypt/decrypt example:
iv := []byte("abcdefg123456789") iv := []byte("abcdefg123456789")
c, err := gnutls.NewCipher(gnutls.GNUTLS_CIPHER_AES_128_CBC, key, iv) c, err := gnutls.NewCipher(gnutls.GNUTLS_CIPHER_AES_128_CBC, key, iv)
if err != nil { if err != nil {
t.Fatal(err) log.Fatal(err)
} }
defer c.Close() defer c.Close()
c1, err := gnutls.NewCipher(gnutls.GNUTLS_CIPHER_AES_128_CBC, key, iv) c1, err := gnutls.NewCipher(gnutls.GNUTLS_CIPHER_AES_128_CBC, key, iv)
if err != nil { if err != nil {
t.Fatal(err) log.Fatal(err)
} }
defer c1.Close() defer c1.Close()
data := []byte("1234012121212121") data := []byte("1234012121212121")
if c == nil { if c == nil {
t.Fatal("new ciphoer failed") log.Fatal("new ciphoer failed")
} }
// encrypt // encrypt
dst := make([]byte, len(data)) dst := make([]byte, len(data))
err := c.Encrypt(dst, data) err := c.Encrypt(dst, data)
if err != nil { if err != nil {
t.Fatal("encrypt failed", err) log.Fatal("encrypt failed", err)
} }
// decrypt // decrypt
data1 := make([]byte, len(data)) data1 := make([]byte, len(data))
err := c1.Decrypt(data1, cdata) err := c1.Decrypt(data1, cdata)
if err != nil { if err != nil {
t.Fatal("decrypt failed", err) log.Fatal("decrypt failed", err)
} }

Loading…
Cancel
Save