From ba9651a36951d6283cdd7282fa823c4e6d79be2b Mon Sep 17 00:00:00 2001 From: hupeh Date: Fri, 13 Oct 2023 14:19:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=91=BD=E4=BB=A4=E8=A1=8C=E5=88=9B?= =?UTF-8?q?=E5=BB=BAticket=20=E5=AF=86=E9=92=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/commands/create_entity.go | 98 ------------------------------ cmd/commands/create_ticket_keys.go | 85 ++++++++++++++++++++++++++ cmd/util/fs.go | 14 +++++ go.mod | 7 ++- go.sum | 30 ++++++--- scripts/rs256/rs256.go | 76 ----------------------- 6 files changed, 125 insertions(+), 185 deletions(-) delete mode 100644 cmd/commands/create_entity.go create mode 100644 cmd/commands/create_ticket_keys.go create mode 100644 cmd/util/fs.go delete mode 100644 scripts/rs256/rs256.go diff --git a/cmd/commands/create_entity.go b/cmd/commands/create_entity.go deleted file mode 100644 index 76d3c0d..0000000 --- a/cmd/commands/create_entity.go +++ /dev/null @@ -1,98 +0,0 @@ -package main - -import ( - "bufio" - "errors" - "fmt" - "os" - "strconv" - "strings" -) - -type StringPrompt struct { - question string - defaultValue string - required bool - confirm bool - validators []func(string) error -} - -func String(question string) *StringPrompt { - return &StringPrompt{question: question} -} - -func Confirm(question string) *StringPrompt { - return &StringPrompt{ - question: question, - confirm: true, - } -} - -func (s *StringPrompt) Required(required bool) *StringPrompt { - s.required = required - return s -} - -func (s *StringPrompt) SetValidator(validators ...func(string) error) *StringPrompt { - s.validators = validators - return s -} - -func (s *StringPrompt) Run() string { - putsln := func(msg string) { - lines := strings.Count(s.question+"\n"+msg, "\n") + 1 - // https://blog.csdn.net/weixin_43988842/article/details/106169040 - fmt.Fprintf(os.Stderr, "\x1b[%dA", lines) // 上移 lines 行 - fmt.Fprint(os.Stderr, "\x1b[J") // 从光标位置到屏幕末尾擦除 - fmt.Fprintln(os.Stderr, msg) - } - - var input string - r := bufio.NewReader(os.Stdin) - -WHILE: - fmt.Fprint(os.Stderr, s.question+" ") - input, _ = r.ReadString('\n') - input = strings.TrimSpace(input) - if input == "" { - if !s.required { - putsln(s.question) - return "" - } - putsln("请输入内容!") - goto WHILE - } - for _, v := range s.validators { - err := v(input) - if err != nil { - putsln(err.Error()) - goto WHILE - } - } - if s.confirm && len(s.validators) == 0 { - switch strings.ToLower(input) { - case "y", "yes": - input = "y" - case "n", "no": - input = "n" - default: - input = "" - } - } - putsln(s.question + " " + input) - return input -} - -func main() { - name := String("What is your name?"). - Required(true). - SetValidator(func(s string) error { - _, err := strconv.ParseInt(s, 10, 64) - if err != nil { - return errors.New("请输入一个数字") - } - return nil - }). - Run() - fmt.Printf("Hello, %s!\n", name) -} diff --git a/cmd/commands/create_ticket_keys.go b/cmd/commands/create_ticket_keys.go new file mode 100644 index 0000000..d7d008b --- /dev/null +++ b/cmd/commands/create_ticket_keys.go @@ -0,0 +1,85 @@ +package main + +import ( + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/asn1" + "encoding/gob" + "encoding/pem" + "os" +) + +// TicketKeysGenerator 创建 ticket 密钥 +type TicketKeysGenerator struct{} + +func (g *TicketKeysGenerator) Generate() (err error) { + defer func() { + if recovered := recover(); recovered != nil { + err = recovered.(error) + } + }() + + reader := rand.Reader + bitSize := 2048 + + key, err := rsa.GenerateKey(reader, bitSize) + g.checkError(err) + + publicKey := key.PublicKey + + g.saveGobKey("private.key", key) + g.savePEMKey("private.pem", key) + + g.saveGobKey("public.key", publicKey) + g.savePublicPEMKey("public.pem", publicKey) + + return +} + +func (g *TicketKeysGenerator) saveGobKey(fileName string, key interface{}) { + outFile, err := os.Create(fileName) + g.checkError(err) + defer outFile.Close() + + encoder := gob.NewEncoder(outFile) + err = encoder.Encode(key) + g.checkError(err) +} + +func (g *TicketKeysGenerator) savePEMKey(fileName string, key *rsa.PrivateKey) { + outFile, err := os.Create(fileName) + g.checkError(err) + defer outFile.Close() + + var privateKey = &pem.Block{ + Type: "PRIVATE KEY", + Bytes: x509.MarshalPKCS1PrivateKey(key), + } + + err = pem.Encode(outFile, privateKey) + g.checkError(err) +} + +func (g *TicketKeysGenerator) savePublicPEMKey(fileName string, pubkey rsa.PublicKey) { + asn1Bytes, err := asn1.Marshal(pubkey) + g.checkError(err) + + var pemkey = &pem.Block{ + Type: "PUBLIC KEY", + Bytes: asn1Bytes, + } + + pemfile, err := os.Create(fileName) + g.checkError(err) + defer pemfile.Close() + + err = pem.Encode(pemfile, pemkey) + g.checkError(err) +} + +func (g *TicketKeysGenerator) checkError(err error) { + if err != nil { + panic(err) + } +} diff --git a/cmd/util/fs.go b/cmd/util/fs.go new file mode 100644 index 0000000..b28d942 --- /dev/null +++ b/cmd/util/fs.go @@ -0,0 +1,14 @@ +package util + +import "os" + +func PathExists(path string) bool { + _, err := os.Stat(path) + if err == nil { + return true + } + if os.IsNotExist(err) { + return false + } + panic(err) +} diff --git a/go.mod b/go.mod index 43400cd..e340d29 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,15 @@ module sorbet go 1.21 require ( + github.com/AlecAivazis/survey/v2 v2.3.7 github.com/go-resty/resty/v2 v2.7.0 github.com/golang-jwt/jwt/v5 v5.0.0 github.com/joho/godotenv v1.5.1 github.com/labstack/echo/v4 v4.11.1 github.com/labstack/gommon v0.4.0 - github.com/manifoldco/promptui v0.9.0 github.com/mattn/go-isatty v0.0.19 github.com/rs/xid v1.5.0 + github.com/stoewer/go-strcase v1.3.0 golang.org/x/time v0.3.0 gorm.io/driver/mysql v1.5.1 gorm.io/driver/postgres v1.5.2 @@ -21,7 +22,6 @@ require ( ) require ( - github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect github.com/go-sql-driver/mysql v1.7.1 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect @@ -31,8 +31,10 @@ require ( github.com/jackc/pgx/v5 v5.3.1 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-sqlite3 v1.14.17 // indirect + github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect github.com/microsoft/go-mssqldb v1.1.0 // indirect github.com/stretchr/testify v1.8.2 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect @@ -40,5 +42,6 @@ require ( golang.org/x/crypto v0.11.0 // indirect golang.org/x/net v0.12.0 // indirect golang.org/x/sys v0.12.0 // indirect + golang.org/x/term v0.10.0 // indirect golang.org/x/text v0.11.0 // indirect ) diff --git a/go.sum b/go.sum index e549662..e17d315 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,13 @@ +github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkkhIiSjQ= +github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.6.0/go.mod h1:bjGvMhVMb+EEm3VRNQawDMUyMMjo+S5ewNjflkep/0Q= github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0/go.mod h1:OQeznEEkTZ9OrhHJoDD8ZDq51FHgXjqtP9z6bEwBq9U= github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0/go.mod h1:okt5dMMTOFjX/aovMlrjvvXoPMBVSPzk9185BT0+eZM= github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0/go.mod h1:kgDmCTgBzIEPFElEF+FK0SdjAor06dRq2Go927dnQ6o= -github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE= -github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= -github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8= -github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= -github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8= -github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s= +github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w= +github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI= +github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -33,6 +33,8 @@ github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+ github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= +github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= @@ -51,22 +53,26 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUUs4= github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ= github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8= github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM= -github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA= -github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4= +github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/microsoft/go-mssqldb v1.1.0 h1:jsV+tpvcPTbNNKW0o3kiCD69kOHICsfjZ2VcVu2lKYc= github.com/microsoft/go-mssqldb v1.1.0/go.mod h1:LzkFdl4z2Ck+Hi+ycGOTbL56VEfgoyA2DvYejrNGbRk= github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8= @@ -76,11 +82,14 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AVEzs= +github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= @@ -116,8 +125,8 @@ golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -138,10 +147,13 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c= +golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= diff --git a/scripts/rs256/rs256.go b/scripts/rs256/rs256.go deleted file mode 100644 index f8cc9cc..0000000 --- a/scripts/rs256/rs256.go +++ /dev/null @@ -1,76 +0,0 @@ -package main - -import ( - "crypto/rand" - "crypto/rsa" - "crypto/x509" - "encoding/asn1" - "encoding/gob" - "encoding/pem" - "fmt" - "os" -) - -func main() { - reader := rand.Reader - bitSize := 2048 - - key, err := rsa.GenerateKey(reader, bitSize) - checkError(err) - - publicKey := key.PublicKey - - saveGobKey("private.key", key) - savePEMKey("private.pem", key) - - saveGobKey("public.key", publicKey) - savePublicPEMKey("public.pem", publicKey) -} - -func saveGobKey(fileName string, key interface{}) { - outFile, err := os.Create(fileName) - checkError(err) - defer outFile.Close() - - encoder := gob.NewEncoder(outFile) - err = encoder.Encode(key) - checkError(err) -} - -func savePEMKey(fileName string, key *rsa.PrivateKey) { - outFile, err := os.Create(fileName) - checkError(err) - defer outFile.Close() - - var privateKey = &pem.Block{ - Type: "PRIVATE KEY", - Bytes: x509.MarshalPKCS1PrivateKey(key), - } - - err = pem.Encode(outFile, privateKey) - checkError(err) -} - -func savePublicPEMKey(fileName string, pubkey rsa.PublicKey) { - asn1Bytes, err := asn1.Marshal(pubkey) - checkError(err) - - var pemkey = &pem.Block{ - Type: "PUBLIC KEY", - Bytes: asn1Bytes, - } - - pemfile, err := os.Create(fileName) - checkError(err) - defer pemfile.Close() - - err = pem.Encode(pemfile, pemkey) - checkError(err) -} - -func checkError(err error) { - if err != nil { - fmt.Println("Fatal error ", err.Error()) - os.Exit(1) - } -}