工作,学习,生活,这里将会有一些记录. 备用域名:http://meisw.wdlinux.cn 注册 | 登陆
浏览模式: 标准 | 列表全部文章

Golang1.7.3使用x509标准库创建自签名证书和签发名其他证书

主代码: package rsa  import (     "crypto/rand"     "crypto/rsa"     "crypto/x509"     "crypto/x509/pkix"     "encoding/pem"     "io/ioutil"     "math/big"     rd "math/rand"     "os"     "time" )  func init() {     rd.Seed(time.Now().UnixNano()) }  type CertInformation struct {     Country            []string     Organization       []string     OrganizationalUnit []string     EmailAddress       []string     Province           []string     Locality           []string     CommonName         string     CrtName, KeyName   string     IsCA               bool     Names              []pkix.AttributeTypeAndValue }  func CreateCRT(RootCa *x509.Certificate, RootKey *rsa.PrivateKey, info CertInformation) error {     Crt := newCertificate(info)     Key, err := rsa.GenerateKey(rand.Reader, 2048)     if err != nil {         return err     }      var buf []byte     if RootCa == nil || RootKey == nil {         //创建自签名证书         buf, err = x509.CreateCertificate(rand.Reader, Crt, Crt, &Key.PublicKey, Key)     } else {         //使用根证书签名         buf, err = x509.CreateCertificate(rand.Reader, Crt, RootCa, &Key.PublicKey, RootKey)     }     if err != nil {         return err     }      err = write(info.CrtName, "CERTIFICATE", buf)     if err != nil {         return err     }      buf = x509.MarshalPKCS1PrivateKey(Key)     return write(info.KeyName, "PRIVATE KEY", buf) } //编码写入文件 func write(filename, Type string, p []byte) error {     File, err := os.Create(filename)     defer File.Close()     if err != nil {         return err     }     var b *pem.Block = &pem.Block{Bytes: p, Type: Type}     return pem.Encode(File, b) }  func Parse(crtPath, keyPath string) (rootcertificate *x509.Certificate, rootPrivateKey *rsa.PrivateKey, err error) {     rootcertificate, err = ParseCrt(crtPath)     if err != nil {         return     }     rootPrivateKey, err = ParseKey(keyPath)     return }  func ParseCrt(path string) (*x509.Certificate, error) {     buf, err := ioutil.ReadFile(path)     if err != nil {         return nil, err     }     p := &pem.Block{}     p, buf = pem.Decode(buf)     return x509.ParseCertificate(p.Bytes) }  func ParseKey(path string) (*rsa.PrivateKey, error) {     buf, err := ioutil.ReadFile(path)     if err != nil {         return nil, err     }     p, buf := pem.Decode(buf)     return x509.ParsePKCS1PrivateKey(p.Bytes) }  func newCertificate(info CertInformation) *x509.Certificate {     return &x509.Certificate{         SerialNumber: big.NewInt(rd.Int63()),         Subject: pkix.Name{             Country:            info.Country,             Organization:       info.Organization,             OrganizationalUnit: info.OrganizationalUnit,             Province:           info.Province,             CommonName:         info.CommonName,             Locality:           info.Locality,             ExtraNames:         info.Names,         },         NotBefore:             time.Now(),//证书的开始时间         NotAfter:              time.Now().AddDate(20, 0, 0),//证书的结束时间         BasicConstraintsValid: true, //基本的有效性约束         IsCA:           info.IsCA,   //是否是根证书         ExtKeyUsage:    []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},    //证书用途         KeyUsage:       x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,         EmailAddresses: info.EmailAddress,     } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
测试代码: package rsa  import (     "crypto/x509/pkix"     "encoding/asn1"     "os"     "testing" )  func Test_crt(t *testing.T) {     baseinfo := CertInformation{Country: []string{"CN"}, Organization: []string{"WS"}, IsCA: true,         OrganizationalUnit: []string{"work-stacks"}, EmailAddress: []string{"czxichen@163.com"},         Locality: []string{"SuZhou"}, Province: []string{"JiangSu"}, CommonName: "Work-Stacks",         CrtName: "test_root.crt", KeyName: "test_root.key"}      err := CreateCRT(nil, nil, baseinfo)     if err != nil {         t.Log("Create crt error,Error info:", err)         return     }     crtinfo := baseinfo     crtinfo.IsCA = false     crtinfo.CrtName = "test_server.crt"     crtinfo.KeyName = "test_server.key"     crtinfo.Names = []pkix.AttributeTypeAndValue{{asn1.ObjectIdentifier{2, 1, 3}, "MAC_ADDR"}} //添加扩展字段用来做自定义使用      crt, pri, err := Parse(baseinfo.CrtName, baseinfo.KeyName)     if err != nil {         t.Log("Parse crt error,Error info:", err)         return     }     err = CreateCRT(crt, pri, crtinfo)     if err != nil {         t.Log("Create crt error,Error info:", err)     }     os.Remove(baseinfo.CrtName)     os.Remove(baseinfo.KeyName)     os.Remove(crtinfo.CrtName)     os.Remove(crtinfo.KeyName) }

 

http://blog.csdn.net/fyxichen/article/details/53010255

 

Go加密解密之RSA

 安全总是很重要的,各个语言对于通用的加密算法都会有实现。前段时间,用Go实现了RSA和DES的加密解密,在这分享一下。(对于RSA和DES加密算法本身,请查阅相关资料)

在PHP中,很多功能经常是一个函数解决;而Go中的却不是。本文会通过PHP加密,Go解密;Go加密,PHP解密来学习Go的RSA和DES相关的API。

该文讨论Go RSA加密解密。所有操作在linux下完成。

一、概要

这是一个非对称加密算法,一般通过公钥加密,私钥解密。

在加解密过程中,使用openssl生产密钥。执行如下操作:

1)创建私钥
openssl genrsa -out private.pem 1024 //密钥长度,1024觉得不够安全的话可以用2048,但是代价也相应增大
2)创建公钥
openssl rsa -in private.pem -pubout -out public.pem

这样便生产了密钥。

一般地,各个语言也会提供API,用于生成密钥。在Go中,可以查看encoding/pem包和crypto/x509包。具体怎么产生,可查看《GO加密解密RSA番外篇:生成RSA密钥》

加密解密这块,涉及到很多标准,个人建议需要的时候临时学习一下。

http://blog.studygolang.com/2013/01/go%E5%8A%A0%E5%AF%86%E8%A7%A3%E5%AF%86%E4%B9%8Brsa/

-------------------

http://www.golangtc.com/t/54c0c8ff421aa95374000091

golang 生成RSA公钥和私钥文件

 package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"os"

)

func main() {
//rsa 密钥文件产生
GenRsaKey(1024)
}
//RSA公钥私钥产生
func GenRsaKey(bits int) error {
// 生成私钥文件
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return err
}
derStream := x509.MarshalPKCS1PrivateKey(privateKey)
block := &pem.Block{
Type:  "RSA PRIVATE KEY",
Bytes: derStream,
}
file, err := os.Create("private.pem")
if err != nil {
return err
}
err = pem.Encode(file, block)
if err != nil {
return err
}
// 生成公钥文件
publicKey := &privateKey.PublicKey
derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return err
}
block = &pem.Block{
Type:  "PUBLIC KEY",
Bytes: derPkix,
}
file, err = os.Create("public.pem")
if err != nil {
return err
}
err = pem.Encode(file, block)
if err != nil {
return err
}
return nil
}

在Go语言中使用JSON

 注意,这里 json的 struct field's tag 规范定义如下:

在Go语言里,StructTag是一个标记字符串,此字符串可跟随在Struct中字段定义的后面。

StructTag就是一系列的 key:”value” 形式的组合,其中key是一个不可为空的字符串,key-value组合可以有多个,空格分隔。

StructTag有什么用?!StructTag主要解决了不同类型数据集合间(Struct,Json,Table等)转换中键值Key定义不一样的问题。StructTag可以理解为一个不同数据类型键值Key的映射表Map, 在StructTag中可以定义不同数据集合键值和Struct中Key值的映射关系,这样方便了Struct数据转为其他类型数据的过程。

在StructTag中加入”omitempty”, 标识该字段的数据可忽略。

- 指定到一个field时,无论有没有值将Person序列化成json时都会忽略该字段 

参考下面代码, 代码来自:http://studygolang.com/articles/1698

 

//tag中的第一个参数是用来指定别名 
//比如Name 指定别名为 username `json:"username"` 
//如果不想指定别名但是想指定其他参数用逗号来分隔 
//omitempty 指定到一个field时 如果在赋值时对该属性赋值 或者 对该属性赋值为 zero value 
//那么将Person序列化成json时会忽略该字段 
//- 指定到一个field时 
//无论有没有值将Person序列化成json时都会忽略该字段 
//string 指定到一个field时 
//比如Person中的Count为int类型 如果没有任何指定在序列化 
//到json之后也是int 比如这个样子 "Count":0 
//但是如果指定了string之后序列化之后也是string类型的 
//那么就是这个样子"Count":"0" 

http://www.cnblogs.com/ghj1976/p/4281793.html


---------------

Encode

将一个对象编码成JSON数据,接受一个interface{}对象,返回[]byte和error:

func Marshal(v interface{}) ([]byte, error)

Marshal函数将会递归遍历整个对象,依次按成员类型对这个对象进行编码,类型转换规则如下:

bool类型 转换为JSON的Boolean
整数,浮点数等数值类型 转换为JSON的Number
string 转换为JSON的字符串(带""引号)
struct 转换为JSON的Object,再根据各个成员的类型递归打包
数组或切片 转换为JSON的Array
[]byte 会先进行base64编码然后转换为JSON字符串
map 转换为JSON的Object,key必须是string
interface{} 按照内部的实际类型进行转换
nil 转为JSON的null
channel,func等类型 会返回UnsupportedTypeError

http://blog.csdn.net/tiaotiaoyly/article/details/38942311

iptables

 :syn-flood - [0:0]

...
-A INPUT -p icmp -m limit --limit 1/sec --limit-burst 10 -j ACCEPT
-A INPUT -f -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
-A syn-flood -j REJECT --reject-with icmp-port-unreachable

开源直播与监控系统

 优秀开源项目之一:视频监控系统iSpy

http://blog.csdn.net/kdlipm/article/details/62423123
 
优秀开源项目之二:流媒体直播系统Open Broadcaster Software
 
http://blog.csdn.net/kdlipm/article/details/62423283

go get 获得 golang.org 的项目

 go get 用来动态获取远程代码包的,目前支持的有BitBucket、GitHub、Google Code和Launchpad。这个命令在内部实际上分成了两步操作:第一步是下载源码包,第二步是执行go install。下载源码包的go工具会自动根据不同的域名调用不同的源码工具,对应关系如下:

BitBucket (Mercurial Git) 
GitHub (Git) 
Google Code Project Hosting (Git, Mercurial, Subversion) 
Launchpad (Bazaar)

go get 的参数说明:

-d 只下载不安装 
-f 只有在你包含了-u参数的时候才有效,不让-u去验证import中的每一个都已经获取了,这对于本地fork的包特别有用 
-fix 在获取源码之后先运行fix,然后再去做其他的事情 
-t 同时也下载需要为运行测试所需要的包 
-u 强制使用网络去更新包和它的依赖包 
-v 显示执行的命令 
注意,这里的 –v 参数对我们分析问题很有帮助。

参考:https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/01.3.md

 

国内由于墙,我们会收到 unrecognized import path 的错误,这时候我们如何通过命令行来执行 go get 呢? 
这时我们会获得类似如下错误:

go get -u -v golang.org/x/oauth2 
Fetching https://golang.org/x/oauth2?go-get=1 
https fetch failed. 
import "golang.org/x/oauth2": https fetch: Get https://golang.org/x/oauth2?go-get=1: dial tcp 216.58.221.145:443: i/o timeout 
package golang.org/x/oauth2: unrecognized import path "golang.org/x/oauth2" 
localhost:~ ghj1976$

如果目录下有以前的版本,则是如下情况:

go get -u -v golang.org/x/oauth2 
Fetching https://golang.org/x/oauth2?go-get=1 
https fetch failed. 
import "golang.org/x/oauth2": https fetch: Get https://golang.org/x/oauth2?go-get=1: dial tcp 216.58.221.145:443: i/o timeout 
golang.org/x/oauth2 (download) 
Fetching https://golang.org/x/net/context?go-get=1 
https fetch failed. 
import "golang.org/x/net/context": https fetch: Get https://golang.org/x/net/context?go-get=1: dial tcp 216.58.221.145:443: i/o timeout 
golang.org/x/net (download) 
Fetching https://golang.org/x/oauth2/internal?go-get=1 
https fetch failed. 
import "golang.org/x/oauth2/internal": https fetch: Get https://golang.org/x/oauth2/internal?go-get=1: dial tcp 216.58.221.145:443: i/o timeout 
golang.org/x/net/context 
golang.org/x/oauth2/internal 
golang.org/x/oauth2 
localhost:~ ghj1976$

 

这时候我们需要设置代理。代理工具我推荐用 lantern https://github.com/getlantern/lantern

需要注意的是,它的代理地址是: http://127.0.0.1:8787   而不是 http://127.0.0.1:16823/ ,后一个是它的配置网站地址。

 

以mac为例, 在命令行 Terminal 中设置网络代理,一般方法如下:

root@ed27c545f7af:~# cat ~/proxy.conf  export http_proxy=http://172.17.42.1:8118 export https_proxy=$http_proxy export ftp_proxy=$http_proxy export rsync_proxy=$http_proxy export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"

参考:https://github.com/tools/godep/issues/154

win下 用 set 代理 export ,参考 https://groups.google.com/forum/#!topic/lantern-users-zh/FiywFrEHSHE 

删除环境变量用

删除:unset 变量名    参考 http://blog.csdn.net/debug_cpp/article/details/2679991 

当前系统上下文的环境设置可以用 env 命令查看。

image

https://code.google.com/p/go/issues/detail?id=2919

这步代理设置后,我们可以用 wget 命令去试验效果。参考: https://github.com/getlantern/lantern/issues/3341 

 

 

另外,go get 使用的 git 、mercurial、svn 设置代理的方法请参考:

https://github.com/golang/go/wiki/GoGetProxyConfig 

以我们最常用的 git 为例,

在终端设置: 
git config --global http.proxy http://127.0.0.1:1080 
git config --global https.proxy https://127.0.0.1:1080

默认不设置代理: 
git config --global --unset http.proxy 
git config --global --unset https.proxy

查看已经设置的值:

git config http.proxy

 

参考: http://blog.csdn.net/dengbin9009/article/details/38058153 

 

配置完成后,以下载 golang.org/x/net 为例,执行的返回值如下:

go get -u -v  golang.org/x/net 
Fetching https://golang.org/x/net?go-get=1 
Parsing meta tags from https://golang.org/x/net?go-get=1 (status code 200) 
get "golang.org/x/net": found meta tag main.metaImport{Prefix:"golang.org/x/net", VCS:"git", RepoRoot:"https://go.googlesource.com/net"} athttps://golang.org/x/net?go-get=1 
golang.org/x/net (download) 
package golang.org/x/net: no buildable Go source files in /Users/ghj1976/project/mygocode/src/golang.org/x/net 
localhost:text ghj1976$

 

我们可以看到其实是到 https://go.googlesource.com/text/ 这样的地址去下载源码的。中间涉及到跳转和git下载,所以 要注意, 网络请求的 http_proxy  和 git 的 代理 都需要设置才可以。

-----------------

命令行设置代理,确保可以下载的命令如下:

export http_proxy=http://127.0.0.1:8787 

git config --global http.proxy http://127.0.0.1:8787 
git config --global https.proxy https://127.0.0.1:8787

 

--------

go get -u golang.org/x/mobile

取消设置

unset http_proxy 
git config --global --unset http.proxy 
git config --global --unset https.proxy

------------

查看设置的变量

echo $http_proxy
git config http.proxy
git config https.proxy

go get使用代理

 git config –global http.proxy “127.0.0.1:8087”

go get …

或者可以在go get的同时指定代理:
http_proxy=127.0.0.1:8087 go get

------------------------------------

我的FQ方式(也可以使用别的方式):

  使用 ishadowsocks 方式FQ

 

临时设置Windows下代理:

  在控制台执行如下命令,后面的的代理值根据你具体的代理进行设置

set http_proxy=http://127.0.0.1:1080/pac?t=201603231602138322 set https_proxy=https://127.0.0.1:1080/pac?t=201603231602138322

 

临时设置Linux下代理:

  在控制台执行如下命令,后面的的代理值根据你具体的代理进行设置

http_proxy=http://127.0.0.1:1080/pac?t=201603231602138322 https_proxy=https://127.0.0.1:1080/pac?t=201603231602138322

 

此时,在控制台执行 go get 时即自动会通过代理。