master
兔子 9 months ago
parent 231cad54d0
commit 033d327f4f

@ -10,7 +10,7 @@ import (
"b612.me/staros" "b612.me/staros"
) )
func EncodeStr(str string, key []byte) string { func EncodeStr(str string, key []byte) (string, error) {
if len(key) < 16 { if len(key) < 16 {
key = starcrypto.Md5(key) key = starcrypto.Md5(key)
} else { } else {
@ -19,19 +19,25 @@ func EncodeStr(str string, key []byte) string {
key = key[:32] key = key[:32]
} }
} }
ensdata := starcrypto.AesEncryptCFBNoBlock([]byte(str), key) ensdata, err := starcrypto.CustomEncryptAesCFB([]byte(str), key)
return starcrypto.Base91EncodeToString(ensdata) if err != nil {
return "", err
}
return starcrypto.Base64Encode(ensdata), nil
} }
func DecodeStr(str string, key []byte) string { func DecodeStr(str string, key []byte) (string, error) {
if len(key) < 16 { if len(key) < 16 {
key = starcrypto.Md5(key) key = starcrypto.Md5(key)
} else { } else {
key = key[:len(key)/16*16] key = key[:len(key)/16*16]
} }
strtmp := starcrypto.Base91DecodeString(str) strtmp, err := starcrypto.Base64Decode(str)
str = string(strtmp) if err != nil {
return string(starcrypto.AesDecryptCFBNoBlock([]byte(str), key)) return "", err
}
p, err := starcrypto.CustomDecryptAesCFB(strtmp, key)
return string(p), err
} }
func EncodeFile(fpath, out string, key []byte) error { func EncodeFile(fpath, out string, key []byte) error {
@ -64,8 +70,13 @@ func EncodeFile(fpath, out string, key []byte) error {
} }
fmt.Printf("已完成:%.2f%%\r", float64(sumAll)/float64(stat.Size())*100) fmt.Printf("已完成:%.2f%%\r", float64(sumAll)/float64(stat.Size())*100)
sumAll += int64(n) sumAll += int64(n)
encodeBytes := starcrypto.AesEncryptCFBNoBlock(buf[:n], key) if n > 0 {
encodeBytes, err := starcrypto.CustomEncryptAesCFBNoBlock(buf[:n], key, key)
if err != nil {
return err
}
fpdst.Write(encodeBytes) fpdst.Write(encodeBytes)
}
if err == io.EOF { if err == io.EOF {
fmt.Print("已完成100% \n") fmt.Print("已完成100% \n")
break break
@ -104,8 +115,13 @@ func DecodeFile(fpath, out string, key []byte) error {
} }
fmt.Printf("已完成:%.2f%%\r", float64(sumAll)/float64(stat.Size())*100) fmt.Printf("已完成:%.2f%%\r", float64(sumAll)/float64(stat.Size())*100)
sumAll += int64(n) sumAll += int64(n)
encodeBytes := starcrypto.AesDecryptCFBNoBlock(buf[:n], key) if n > 0 {
encodeBytes, err := starcrypto.CustomDecryptAesCFBNoBlock(buf[:n], key, key)
if err != nil {
return err
}
fpdst.Write(encodeBytes) fpdst.Write(encodeBytes)
}
if err == io.EOF { if err == io.EOF {
fmt.Print("已完成100% \n") fmt.Print("已完成100% \n")
break break

@ -14,9 +14,9 @@ import (
) )
var Cmd = &cobra.Command{ var Cmd = &cobra.Command{
Use: "aes-cfb", Use: "aescfb",
Short: "使用aes-cfb处理文件或字符串", Short: "使用自定义aes-cfb处理文件或字符串",
Long: "使用aes-cfb处理文件或字符串", Long: "使用自定义aes-cfb处理文件或字符串",
Run: func(this *cobra.Command, args []string) { Run: func(this *cobra.Command, args []string) {
var err error var err error
ok, _ := this.Flags().GetBool("file") ok, _ := this.Flags().GetBool("file")
@ -70,10 +70,18 @@ var Cmd = &cobra.Command{
} }
} else { } else {
if !de { if !de {
data := EncodeStr(args[0], byteKey) data, err := EncodeStr(args[0], byteKey)
if err != nil {
starlog.Criticalln(err)
return
}
fmt.Println(data) fmt.Println(data)
} else { } else {
data := DecodeStr(args[0], byteKey) data, err := DecodeStr(args[0], byteKey)
if err != nil {
starlog.Criticalln(err)
return
}
fmt.Println(string(data)) fmt.Println(string(data))
} }
} }

@ -1,11 +1,13 @@
package cert package cert
import ( import (
"b612.me/starcrypto"
"b612.me/starlog" "b612.me/starlog"
"crypto/dsa" "crypto/dsa"
"crypto/ecdh" "crypto/ecdh"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/ed25519" "crypto/ed25519"
"crypto/rand"
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
@ -294,7 +296,59 @@ func ParseCert(data []byte, pwd string) {
default: default:
starlog.Green("未知公钥类型\n") starlog.Green("未知公钥类型\n")
} }
return continue
case "RSA PRIVATE KEY":
n, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
starlog.Errorf("解析私钥错误:%s\n", err)
continue
}
starlog.Infof("这是一个RSA私钥文件\n")
starlog.Green("私钥位数:%d\n", n.Size())
starlog.Green("私钥长度:%d\n", n.N.BitLen())
starlog.Green("私钥指数:%d\n", n.E)
starlog.Green("私钥系数:%d\n", n.D)
starlog.Green("私钥质数p%d\n", n.Primes[0])
starlog.Green("私钥质数q%d\n", n.Primes[1])
starlog.Green("私钥系数dP%d\n", n.Precomputed.Dp)
starlog.Green("私钥系数dQ%d\n", n.Precomputed.Dq)
starlog.Green("私钥系数qInv%d\n", n.Precomputed.Qinv)
case "RSA PUBLIC KEY":
n, err := x509.ParsePKCS1PublicKey(block.Bytes)
if err != nil {
starlog.Errorf("解析公钥错误:%s\n", err)
continue
}
starlog.Green("这是一个RSA公钥\n")
starlog.Green("公钥位数:%d\n", n.Size())
starlog.Green("公钥长度:%d\n", n.N.BitLen())
starlog.Green("公钥指数:%d\n", n.E)
case "EC PRIVATE KEY":
n, err := x509.ParseECPrivateKey(block.Bytes)
if err != nil {
starlog.Errorf("解析私钥错误:%s\n", err)
continue
}
starlog.Green("这是一个ECDSA私钥\n")
starlog.Green("私钥位数:%d\n", n.Curve.Params().BitSize)
starlog.Green("私钥曲线:%s\n", n.Curve.Params().Name)
starlog.Green("私钥长度:%d\n", n.Params().BitSize)
starlog.Green("私钥系数:%d\n", n.D)
starlog.Green("私钥公钥X%d\n", n.PublicKey.X)
starlog.Green("私钥公钥Y%d\n", n.PublicKey.Y)
case "EC PUBLIC KEY":
p, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
starlog.Errorf("解析公钥错误:%s\n", err)
continue
}
n := p.(*ecdsa.PublicKey)
starlog.Green("这是一个ECDSA公钥\n")
starlog.Green("公钥位数:%d\n", n.Curve.Params().BitSize)
starlog.Green("公钥曲线:%s\n", n.Curve.Params().Name)
starlog.Green("公钥长度:%d\n", n.Params().BitSize)
starlog.Green("公钥公钥X%d\n", n.X)
starlog.Green("公钥公钥Y%d\n", n.Y)
default: default:
starlog.Infof("未知证书文件类型\n") starlog.Infof("未知证书文件类型\n")
} }
@ -556,6 +610,62 @@ func GetCert(data []byte, pwd string) ([]any, []x509.Certificate, error) {
starlog.Green("未知公钥类型\n") starlog.Green("未知公钥类型\n")
} }
return common, certs, nil return common, certs, nil
case "RSA PRIVATE KEY":
n, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
starlog.Errorf("解析私钥错误:%s\n", err)
continue
}
starlog.Infof("这是一个RSA私钥文件\n")
starlog.Green("私钥位数:%d\n", n.Size())
starlog.Green("私钥长度:%d\n", n.N.BitLen())
starlog.Green("私钥指数:%d\n", n.E)
starlog.Green("私钥系数:%d\n", n.D)
starlog.Green("私钥质数p%d\n", n.Primes[0])
starlog.Green("私钥质数q%d\n", n.Primes[1])
starlog.Green("私钥系数dP%d\n", n.Precomputed.Dp)
starlog.Green("私钥系数dQ%d\n", n.Precomputed.Dq)
starlog.Green("私钥系数qInv%d\n", n.Precomputed.Qinv)
common = append(common, n)
case "RSA PUBLIC KEY":
n, err := x509.ParsePKCS1PublicKey(block.Bytes)
if err != nil {
starlog.Errorf("解析公钥错误:%s\n", err)
continue
}
starlog.Green("这是一个RSA公钥\n")
starlog.Green("公钥位数:%d\n", n.Size())
starlog.Green("公钥长度:%d\n", n.N.BitLen())
starlog.Green("公钥指数:%d\n", n.E)
common = append(common, n)
case "EC PRIVATE KEY":
n, err := x509.ParseECPrivateKey(block.Bytes)
if err != nil {
starlog.Errorf("解析私钥错误:%s\n", err)
continue
}
starlog.Green("这是一个ECDSA私钥\n")
starlog.Green("私钥位数:%d\n", n.Curve.Params().BitSize)
starlog.Green("私钥曲线:%s\n", n.Curve.Params().Name)
starlog.Green("私钥长度:%d\n", n.Params().BitSize)
starlog.Green("私钥系数:%d\n", n.D)
starlog.Green("私钥公钥X%d\n", n.PublicKey.X)
starlog.Green("私钥公钥Y%d\n", n.PublicKey.Y)
common = append(common, n)
case "EC PUBLIC KEY":
p, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
starlog.Errorf("解析公钥错误:%s\n", err)
continue
}
n := p.(*ecdsa.PublicKey)
starlog.Green("这是一个ECDSA公钥\n")
starlog.Green("公钥位数:%d\n", n.Curve.Params().BitSize)
starlog.Green("公钥曲线:%s\n", n.Curve.Params().Name)
starlog.Green("公钥长度:%d\n", n.Params().BitSize)
starlog.Green("公钥公钥X%d\n", n.X)
starlog.Green("公钥公钥Y%d\n", n.Y)
common = append(common, n)
default: default:
starlog.Infof("未知证书文件类型\n") starlog.Infof("未知证书文件类型\n")
} }
@ -604,7 +714,7 @@ func Pkcs1(data []byte, pwd string, originName string, outpath string) error {
if err != nil { if err != nil {
return err return err
} }
for _, v := range keys { for idx, v := range keys {
if v == nil { if v == nil {
continue continue
} }
@ -614,23 +724,120 @@ func Pkcs1(data []byte, pwd string, originName string, outpath string) error {
if err != nil { if err != nil {
return err return err
} }
err = os.WriteFile(outpath+"/"+originName+".pkcs1", pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: data}), 0644) err = os.WriteFile(fmt.Sprintf("%s/%s_%v.pkcs1", outpath, originName, idx), pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: data}), 0644)
if err != nil { if err != nil {
return err return err
} else { } else {
starlog.Green("已将私钥保存到%s\n", outpath+"/"+originName+".pkcs8") starlog.Green("已将私钥保存到%s\n", fmt.Sprintf("%s/%s_%v.pkcs1", outpath, originName, idx))
} }
case *rsa.PublicKey: case *rsa.PublicKey:
data = x509.MarshalPKCS1PublicKey(n) data = x509.MarshalPKCS1PublicKey(n)
if err != nil { if err != nil {
return err return err
} }
err = os.WriteFile(outpath+"/"+originName+".pub.pkcs1", pem.EncodeToMemory(&pem.Block{Type: "RSA PUBLIC KEY", Bytes: data}), 0644) err = os.WriteFile(fmt.Sprintf("%s/%s_%v.pub.pkcs1", outpath, originName, idx), pem.EncodeToMemory(&pem.Block{Type: "RSA PUBLIC KEY", Bytes: data}), 0644)
if err != nil {
return err
} else {
starlog.Green("已将公钥保存到%s\n", fmt.Sprintf("%s/%s_%v.pub.pkcs1", outpath, originName, idx))
}
}
}
return nil
}
func Pkcs12(keys []any, certs []x509.Certificate, enPwd string, originName string, outpath string) error {
var priv any
for _, v := range keys {
if v == nil {
continue
}
switch n := v.(type) {
case *ecdsa.PrivateKey, *rsa.PrivateKey, *dsa.PrivateKey, *ed25519.PrivateKey, *ecdh.PrivateKey:
priv = n
break
}
}
if priv == nil {
return errors.New("未找到私钥")
}
if len(certs) == 0 {
return errors.New("未找到证书")
}
mainCert := certs[0]
var certChain []*x509.Certificate
for _, v := range certs[1:] {
certChain = append(certChain, &v)
}
pfxData, err := pkcs12.Encode(rand.Reader, priv, &mainCert, certChain, enPwd)
if err != nil {
return err
}
err = os.WriteFile(outpath+"/"+originName+".pfx", pfxData, 0644)
if err != nil {
starlog.Errorf("保存失败:%s\n", err)
return err
}
starlog.Green("已将PKCS12文件保存到%s\n", outpath+"/"+originName+".pfx")
return nil
}
func Tran(data []byte, pwd string, originName string, outpath string) error {
keys, _, err := GetCert(data, pwd)
if err != nil {
return err
}
for idx, v := range keys {
if v == nil {
continue
}
switch n := v.(type) {
case *ecdsa.PrivateKey, *rsa.PrivateKey:
data, err = starcrypto.EncodePrivateKey(n, "")
if err != nil {
return err
}
err = os.WriteFile(fmt.Sprintf("%s/%s_%v.tran.key", outpath, originName, idx), data, 0644)
if err != nil { if err != nil {
return err return err
} else { } else {
starlog.Green("已将公钥保存到%s\n", outpath+"/"+originName+".pub.pkcs1") starlog.Green("已将私钥保存到%s\n", fmt.Sprintf("%s/%s_%v.tran.key", outpath, originName, idx))
} }
case *ecdsa.PublicKey, *rsa.PublicKey:
data, err = starcrypto.EncodePublicKey(n)
if err != nil {
return err
}
err = os.WriteFile(fmt.Sprintf("%s/%s_%v.tran.pub", outpath, originName, idx), data, 0644)
if err != nil {
return err
} else {
starlog.Green("已将公钥保存到%s\n", fmt.Sprintf("%s/%s_%v.tran.pub", outpath, originName, idx))
}
case *dsa.PrivateKey, *ed25519.PrivateKey, *ecdh.PrivateKey:
data, err = x509.MarshalPKCS8PrivateKey(n)
if err != nil {
return err
}
err = os.WriteFile(outpath+"/"+originName+".tran.key", pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: data}), 0644)
if err != nil {
return err
} else {
starlog.Green("已将私钥保存到%s\n", outpath+"/"+originName+".tran.key")
}
case *dsa.PublicKey, *ed25519.PublicKey, *ecdh.PublicKey:
data, err = x509.MarshalPKIXPublicKey(n)
if err != nil {
return err
}
err = os.WriteFile(outpath+"/"+originName+".tran.pub", pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: data}), 0644)
if err != nil {
return err
} else {
starlog.Green("已将公钥保存到%s\n", outpath+"/"+originName+".tran.pub")
}
} }
} }
return nil return nil

@ -4,6 +4,7 @@ import (
"b612.me/starcrypto" "b612.me/starcrypto"
"b612.me/stario" "b612.me/stario"
"b612.me/starlog" "b612.me/starlog"
"crypto/x509"
"fmt" "fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"os" "os"
@ -27,6 +28,7 @@ var csr string
var pubKey string var pubKey string
var caKeyPwd string var caKeyPwd string
var passwd string var passwd string
var enPasswd string
var Cmd = &cobra.Command{ var Cmd = &cobra.Command{
Use: "cert", Use: "cert",
@ -191,11 +193,23 @@ func init() {
CmdGen.Flags().StringVarP(&caKeyPwd, "caKeyPwd", "p", "", "CA私钥密码") CmdGen.Flags().StringVarP(&caKeyPwd, "caKeyPwd", "p", "", "CA私钥密码")
Cmd.AddCommand(CmdGen) Cmd.AddCommand(CmdGen)
CmdParse.Flags().StringVarP(&passwd, "passwd", "p", "", "证书密码") CmdParse.Flags().StringVarP(&passwd, "passwd", "p", "", "pfx解密密码")
Cmd.AddCommand(CmdParse) Cmd.AddCommand(CmdParse)
CmdPkcs8.Flags().StringVarP(&passwd, "passwd", "p", "", "证书密码") CmdPkcs8.Flags().StringVarP(&passwd, "passwd", "p", "", "pfx解密密码")
CmdPkcs8.Flags().StringVarP(&savefolder, "savefolder", "s", "./", "保存文件夹") CmdPkcs8.Flags().StringVarP(&savefolder, "savefolder", "s", ".", "保存文件夹")
Cmd.AddCommand(CmdPkcs8) Cmd.AddCommand(CmdPkcs8)
CmdPkcs1.Flags().StringVarP(&passwd, "passwd", "p", "", "pfx解密密码")
CmdPkcs1.Flags().StringVarP(&savefolder, "savefolder", "s", ".", "保存文件夹")
Cmd.AddCommand(CmdPkcs1)
CmdPkcs12.Flags().StringVarP(&passwd, "passwd", "p", "", "pfx解密密码")
CmdPkcs12.Flags().StringVarP(&enPasswd, "pfx-passwd", "P", "", "pfx加密密码")
CmdPkcs12.Flags().StringVarP(&savefolder, "savefolder", "s", ".", "保存文件夹")
Cmd.AddCommand(CmdPkcs12)
CmdBasic.Flags().StringVarP(&passwd, "passwd", "p", "", "pfx解密密码")
CmdBasic.Flags().StringVarP(&savefolder, "savefolder", "s", ".", "保存文件夹")
Cmd.AddCommand(CmdBasic)
} }
var CmdPkcs8 = &cobra.Command{ var CmdPkcs8 = &cobra.Command{
@ -213,7 +227,96 @@ var CmdPkcs8 = &cobra.Command{
starlog.Errorln("读取证书错误", err) starlog.Errorln("读取证书错误", err)
continue continue
} }
Pkcs8(data, passwd, filepath.Base(v), savefolder) err = Pkcs8(data, passwd, filepath.Base(v), savefolder)
if err != nil {
starlog.Errorln("pkcs8转换错误", err)
continue
}
fmt.Println("\n-------" + v + "转换完毕---------\n")
}
},
}
var CmdPkcs1 = &cobra.Command{
Use: "pkcs1",
Short: "pkcs1转换",
Long: "pkcs1转换",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
starlog.Errorln("请输入证书文件")
os.Exit(1)
}
for _, v := range args {
data, err := os.ReadFile(v)
if err != nil {
starlog.Errorln("读取证书错误", err)
continue
}
err = Pkcs1(data, passwd, filepath.Base(v), savefolder)
if err != nil {
starlog.Errorln("pkcs1转换错误", err)
continue
}
fmt.Println("\n-------" + v + "转换完毕---------\n")
}
},
}
var CmdPkcs12 = &cobra.Command{
Use: "pkcs12",
Short: "pkcs12转换",
Long: "pkcs12转换",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
starlog.Errorln("请输入证书文件")
os.Exit(1)
}
var keys []any
var certs []x509.Certificate
for _, v := range args {
data, err := os.ReadFile(v)
if err != nil {
starlog.Errorln("读取证书错误", err)
continue
}
key, cert, err := GetCert(data, passwd)
if err != nil {
starlog.Errorln("证书读取错误", err)
os.Exit(1)
}
keys = append(keys, key...)
certs = append(certs, cert...)
}
err := Pkcs12(keys, certs, enPasswd, filepath.Base(args[0]), savefolder)
if err != nil {
starlog.Errorln("pkcs12转换错误", err)
os.Exit(1)
}
fmt.Println("\n-------pfk转换完毕---------\n")
},
}
var CmdBasic = &cobra.Command{
Use: "basic",
Short: "证书转换为基本类型",
Long: "证书转换为基本类型",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
starlog.Errorln("请输入证书文件")
os.Exit(1)
}
for _, v := range args {
data, err := os.ReadFile(v)
if err != nil {
starlog.Errorln("读取证书错误", err)
continue
}
err = Tran(data, passwd, filepath.Base(v), savefolder)
if err != nil {
starlog.Errorln("证书转换错误", err)
continue
}
fmt.Println("\n-------" + v + "转换完毕---------\n") fmt.Println("\n-------" + v + "转换完毕---------\n")
} }
}, },

@ -4,7 +4,7 @@ go 1.19
require ( require (
b612.me/notify v1.2.5 b612.me/notify v1.2.5
b612.me/starcrypto v0.0.4 b612.me/starcrypto v0.0.5
b612.me/stario v0.0.9 b612.me/stario v0.0.9
b612.me/starlog v1.3.3 b612.me/starlog v1.3.3
b612.me/starnet v0.1.8 b612.me/starnet v0.1.8
@ -24,6 +24,7 @@ require (
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/spf13/cobra v1.8.0 github.com/spf13/cobra v1.8.0
github.com/things-go/go-socks5 v0.0.5 github.com/things-go/go-socks5 v0.0.5
golang.org/x/crypto v0.21.0
software.sslmate.com/src/go-pkcs12 v0.4.0 software.sslmate.com/src/go-pkcs12 v0.4.0
) )
@ -36,7 +37,6 @@ require (
github.com/kr/fs v0.1.0 // indirect github.com/kr/fs v0.1.0 // indirect
github.com/pkg/sftp v1.13.4 // indirect github.com/pkg/sftp v1.13.4 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/image v0.6.0 // indirect golang.org/x/image v0.6.0 // indirect
golang.org/x/mod v0.14.0 // indirect golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.21.0 // indirect golang.org/x/net v0.21.0 // indirect

@ -1,8 +1,8 @@
b612.me/notify v1.2.5 h1:fASpzi8YAo78g6jKnefzfbsQz0nGNYFbClB2Bylj+MA= b612.me/notify v1.2.5 h1:fASpzi8YAo78g6jKnefzfbsQz0nGNYFbClB2Bylj+MA=
b612.me/notify v1.2.5/go.mod h1:GTnAdC6v9krGxtC8Gkn8TcyUsYnHSiHjRAXsONPiLpI= b612.me/notify v1.2.5/go.mod h1:GTnAdC6v9krGxtC8Gkn8TcyUsYnHSiHjRAXsONPiLpI=
b612.me/starcrypto v0.0.3/go.mod h1:pF5A16p8r/h1G0x7ZNmmAF6K1sdIMpbCUxn2WGC8gZ0= b612.me/starcrypto v0.0.3/go.mod h1:pF5A16p8r/h1G0x7ZNmmAF6K1sdIMpbCUxn2WGC8gZ0=
b612.me/starcrypto v0.0.4 h1:4obj3QC9SoFop9WA3xKNS99WCvRrxZnvEUMBVzrD4HM= b612.me/starcrypto v0.0.5 h1:Aa4pRDO2lBH2Aw+vz8NuUtRb73J8z5aOa9SImBY5sq4=
b612.me/starcrypto v0.0.4/go.mod h1:pF5A16p8r/h1G0x7ZNmmAF6K1sdIMpbCUxn2WGC8gZ0= b612.me/starcrypto v0.0.5/go.mod h1:pF5A16p8r/h1G0x7ZNmmAF6K1sdIMpbCUxn2WGC8gZ0=
b612.me/stario v0.0.9 h1:bFDlejUJMwZ12a09snZJspQsOlkqpDAl9qKPEYOGWCk= b612.me/stario v0.0.9 h1:bFDlejUJMwZ12a09snZJspQsOlkqpDAl9qKPEYOGWCk=
b612.me/stario v0.0.9/go.mod h1:x4D/x8zA5SC0pj/uJAi4FyG5p4j5UZoMEZfvuRR6VNw= b612.me/stario v0.0.9/go.mod h1:x4D/x8zA5SC0pj/uJAi4FyG5p4j5UZoMEZfvuRR6VNw=
b612.me/starlog v1.3.3 h1:xYCHouOTpo6dsFg2A92TqTznxvRPPS/ovMWs7CJZ9WI= b612.me/starlog v1.3.3 h1:xYCHouOTpo6dsFg2A92TqTznxvRPPS/ovMWs7CJZ9WI=

@ -1,15 +1,26 @@
package hash package hash
import ( import (
"b612.me/starcrypto" "crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"golang.org/x/crypto/md4"
"golang.org/x/crypto/ripemd160"
"hash"
"hash/crc32"
"io"
"os" "os"
"b612.me/starlog" "b612.me/starlog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var hmacKey string
var Cmd = &cobra.Command{ var Cmd = &cobra.Command{
Use: "hash", Use: "hash",
Short: "多种方法哈希值校验", Short: "多种方法哈希值校验",
@ -18,7 +29,7 @@ var Cmd = &cobra.Command{
var cumethod, method []string var cumethod, method []string
var result = make(map[string]string) var result = make(map[string]string)
var err error var err error
cumethod = []string{"md5", "crc32", "sha512", "sha384", "sha256", "sha224", "sha1"} cumethod = []string{"md5", "crc32", "sha512", "sha384", "sha256", "sha224", "sha1", "md4", "ripemd160", "hmacmd5", "hmacmd4", "hmacsha1", "hmacsha224", "hmacsha256", "hmacsha384", "hmacsha512"}
if ok, _ := this.Flags().GetBool("all"); ok { if ok, _ := this.Flags().GetBool("all"); ok {
method = cumethod method = cumethod
} else { } else {
@ -37,7 +48,7 @@ var Cmd = &cobra.Command{
} }
} }
if ok, _ := this.Flags().GetBool("file"); ok { if ok, _ := this.Flags().GetBool("file"); ok {
result, err = starcrypto.FileSumAll(args[0], method, func(pect float64) { result, err = FileSumAll(args[0], hmacKey, method, func(pect float64) {
if pect != 100.0 { if pect != 100.0 {
fmt.Printf("校验已完成:%f%%\r", pect) fmt.Printf("校验已完成:%f%%\r", pect)
} else { } else {
@ -46,7 +57,7 @@ var Cmd = &cobra.Command{
}) })
} else { } else {
var bres map[string][]byte var bres map[string][]byte
bres, err = starcrypto.SumAll([]byte(args[0]), method) bres, err = SumAll([]byte(args[0]), hmacKey, method)
if err == nil { if err == nil {
for k, v := range bres { for k, v := range bres {
result[k] = hex.EncodeToString(v) result[k] = hex.EncodeToString(v)
@ -63,6 +74,7 @@ var Cmd = &cobra.Command{
} }
func init() { func init() {
Cmd.Flags().StringVarP(&hmacKey, "hmac-key", "k", "b612.me", "HMAC密钥")
Cmd.Flags().BoolP("all", "a", false, "使用所有的校验方法") Cmd.Flags().BoolP("all", "a", false, "使用所有的校验方法")
Cmd.Flags().BoolP("file", "f", false, "对指定文件进行校验") Cmd.Flags().BoolP("file", "f", false, "对指定文件进行校验")
Cmd.Flags().BoolP("md5", "m", false, "进行MD5校验默认") Cmd.Flags().BoolP("md5", "m", false, "进行MD5校验默认")
@ -72,4 +84,185 @@ func init() {
Cmd.Flags().Bool("sha256", false, "进行SHA256校验") Cmd.Flags().Bool("sha256", false, "进行SHA256校验")
Cmd.Flags().Bool("sha224", false, "进行SHA224校验") Cmd.Flags().Bool("sha224", false, "进行SHA224校验")
Cmd.Flags().Bool("sha1", false, "进行SHA1校验") Cmd.Flags().Bool("sha1", false, "进行SHA1校验")
Cmd.Flags().Bool("md4", false, "进行MD4校验")
Cmd.Flags().Bool("ripemd160", false, "进行RIPEMD160校验")
Cmd.Flags().Bool("hmacmd5", false, "进行HMACMD5校验")
Cmd.Flags().Bool("hmacmd4", false, "进行HMACMD4校验")
Cmd.Flags().Bool("hmacsha1", false, "进行HMACSHA1校验")
Cmd.Flags().Bool("hmacsha224", false, "进行HMACSHA224校验")
Cmd.Flags().Bool("hmacsha256", false, "进行HMACSHA256校验")
Cmd.Flags().Bool("hmacsha384", false, "进行HMACSHA384校验")
Cmd.Flags().Bool("hmacsha512", false, "进行HMACSHA512校验")
}
func FileSumAll(filepath string, key string, method []string, shell func(float64)) (map[string]string, error) {
result := make(map[string]string)
methods := make(map[string]hash.Hash)
var iscrc bool
if len(method) == 0 {
method = []string{"sha512", "sha256", "sha384", "sha224", "sha1", "crc32", "md5", "md4", "ripemd160", "hmacmd5", "hmacmd4", "hmacsha1", "hmacsha224", "hmacsha256", "hmacsha384", "hmacsha512"}
}
fp, err := os.Open(filepath)
defer fp.Close()
if err != nil {
return result, err
}
stat, _ := os.Stat(filepath)
filebig := float64(stat.Size())
sum512 := sha512.New()
sum384 := sha512.New384()
sum256 := sha256.New()
sum224 := sha256.New224()
sum1 := sha1.New()
crcsum := crc32.NewIEEE()
md5sum := md5.New()
md4sum := md4.New()
ripemd160sum := ripemd160.New()
hmacmd5sum := hmac.New(md5.New, []byte(key))
hmacmd4sum := hmac.New(md4.New, []byte(key))
hmacsha1sum := hmac.New(sha1.New, []byte(key))
hmacsha224sum := hmac.New(sha256.New224, []byte(key))
hmacsha256sum := hmac.New(sha256.New, []byte(key))
hmacsha384sum := hmac.New(sha512.New384, []byte(key))
hmacsha512sum := hmac.New(sha512.New, []byte(key))
for _, v := range method {
switch v {
case "md5":
methods["md5"] = md5sum
case "crc32":
iscrc = true
case "sha1":
methods["sha1"] = sum1
case "sha224":
methods["sha224"] = sum224
case "sha256":
methods["sha256"] = sum256
case "sha384":
methods["sha384"] = sum384
case "sha512":
methods["sha512"] = sum512
case "md4":
methods["md4"] = md4sum
case "ripemd160":
methods["ripemd160"] = ripemd160sum
case "hmacmd5":
methods["hmacmd5"] = hmacmd5sum
case "hmacmd4":
methods["hmacmd4"] = hmacmd4sum
case "hmacsha1":
methods["hmacsha1"] = hmacsha1sum
case "hmacsha224":
methods["hmacsha224"] = hmacsha224sum
case "hmacsha256":
methods["hmacsha256"] = hmacsha256sum
case "hmacsha384":
methods["hmacsha384"] = hmacsha384sum
case "hmacsha512":
methods["hmacsha512"] = hmacsha512sum
}
}
writer := 0
for {
buf := make([]byte, 1048574)
n, err := fp.Read(buf)
if err != nil {
if err == io.EOF {
break
}
return result, err
}
writer += n
pect := (float64(writer) / filebig) * 100
go shell(pect)
for _, v := range methods {
v.Write(buf[0:n])
}
if iscrc {
crcsum.Write(buf[0:n])
}
}
for k, v := range methods {
result[k] = hex.EncodeToString(v.Sum(nil))
}
if iscrc {
result["crc32"] = hex.EncodeToString(crcsum.Sum(nil))
}
return result, nil
}
func SumAll(data []byte, key string, method []string) (map[string][]byte, error) {
result := make(map[string][]byte)
methods := make(map[string]hash.Hash)
var iscrc bool
if len(method) == 0 {
method = []string{"sha512", "sha256", "sha384", "sha224", "sha1", "crc32", "md5", "md4", "ripemd160", "hmacmd5", "hmacmd4", "hmacsha1", "hmacsha224", "hmacsha256", "hmacsha384", "hmacsha512"}
}
sum512 := sha512.New()
sum384 := sha512.New384()
sum256 := sha256.New()
sum224 := sha256.New224()
sum1 := sha1.New()
crcsum := crc32.NewIEEE()
md5sum := md5.New()
md4sum := md4.New()
ripemd160sum := ripemd160.New()
hmacmd5sum := hmac.New(md5.New, []byte(key))
hmacmd4sum := hmac.New(md4.New, []byte(key))
hmacsha1sum := hmac.New(sha1.New, []byte(key))
hmacsha224sum := hmac.New(sha256.New224, []byte(key))
hmacsha256sum := hmac.New(sha256.New, []byte(key))
hmacsha384sum := hmac.New(sha512.New384, []byte(key))
hmacsha512sum := hmac.New(sha512.New, []byte(key))
for _, v := range method {
switch v {
case "md5":
methods["md5"] = md5sum
case "crc32":
iscrc = true
case "sha1":
methods["sha1"] = sum1
case "sha224":
methods["sha224"] = sum224
case "sha256":
methods["sha256"] = sum256
case "sha384":
methods["sha384"] = sum384
case "sha512":
methods["sha512"] = sum512
case "md4":
methods["md4"] = md4sum
case "ripemd160":
methods["ripemd160"] = ripemd160sum
case "hmacmd5":
methods["hmacmd5"] = hmacmd5sum
case "hmacmd4":
methods["hmacmd4"] = hmacmd4sum
case "hmacsha1":
methods["hmacsha1"] = hmacsha1sum
case "hmacsha224":
methods["hmacsha224"] = hmacsha224sum
case "hmacsha256":
methods["hmacsha256"] = hmacsha256sum
case "hmacsha384":
methods["hmacsha384"] = hmacsha384sum
case "hmacsha512":
methods["hmacsha512"] = hmacsha512sum
}
}
for _, v := range methods {
v.Write(data)
}
if iscrc {
crcsum.Write(data)
}
for k, v := range methods {
result[k] = v.Sum(nil)
}
if iscrc {
result["crc32"] = crcsum.Sum(nil)
}
return result, nil
} }

@ -23,7 +23,7 @@ var outpath string
var sshPub bool var sshPub bool
func init() { func init() {
Cmd.Flags().StringVarP(&k.Type, "type", "t", "rsa", "Key Type: rsa, ecdsa") Cmd.Flags().StringVarP(&k.Type, "type", "t", "rsa", "Key Type: rsa, ecdsa, ed25519")
Cmd.Flags().StringVarP(&k.Encrypt, "encrypt", "e", "", "Encrypt Key with Password (not recommended)") Cmd.Flags().StringVarP(&k.Encrypt, "encrypt", "e", "", "Encrypt Key with Password (not recommended)")
Cmd.Flags().IntVarP(&k.Bits, "bits", "b", 2048, "Key Bits Rsa: 1024, 2048, 4096 Ecdsa: 224, 256, 384, 521") Cmd.Flags().IntVarP(&k.Bits, "bits", "b", 2048, "Key Bits Rsa: 1024, 2048, 4096 Ecdsa: 224, 256, 384, 521")
Cmd.Flags().StringVarP(&k.Prefix, "prefix", "p", "mykey", "Output File Prefix") Cmd.Flags().StringVarP(&k.Prefix, "prefix", "p", "mykey", "Output File Prefix")

@ -5,6 +5,7 @@ import (
"b612.me/staros" "b612.me/staros"
"crypto" "crypto"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic" "crypto/elliptic"
"crypto/rand" "crypto/rand"
"crypto/rsa" "crypto/rsa"
@ -13,6 +14,7 @@ import (
"encoding/hex" "encoding/hex"
"encoding/pem" "encoding/pem"
"errors" "errors"
"fmt"
"io" "io"
"math/big" "math/big"
"os" "os"
@ -61,7 +63,7 @@ func (k *KeyGen) Gen() error {
if err != nil { if err != nil {
return err return err
} }
case "ecdsa": case "ecdsa", "ecdh":
var cr elliptic.Curve var cr elliptic.Curve
switch k.Bits { switch k.Bits {
case 224: case 224:
@ -79,6 +81,11 @@ func (k *KeyGen) Gen() error {
if err != nil { if err != nil {
return err return err
} }
case "ed25519":
pub, priv, err = ed25519.GenerateKey(rand.Reader)
if err != nil {
return err
}
default: default:
return errors.New("invalid key type,only support rsa,ecdsa") return errors.New("invalid key type,only support rsa,ecdsa")
} }
@ -166,8 +173,10 @@ func (k *KeyGen) GenerateCert(priv crypto.PrivateKey) ([]byte, []byte, error) {
} }
case *ecdsa.PrivateKey: case *ecdsa.PrivateKey:
cert, err = MakeCert(priv.(*ecdsa.PrivateKey), rootCsr, rootCsr, priv.(*ecdsa.PrivateKey).Public()) cert, err = MakeCert(priv.(*ecdsa.PrivateKey), rootCsr, rootCsr, priv.(*ecdsa.PrivateKey).Public())
case ed25519.PrivateKey:
cert, err = MakeCert(priv.(ed25519.PrivateKey), rootCsr, rootCsr, priv.(ed25519.PrivateKey).Public())
default: default:
return nil, nil, errors.New("invalid private key type") return nil, nil, errors.New("invalid private key type:" + fmt.Sprintf("%T", priv))
} }
csrPem := pem.EncodeToMemory(&pem.Block{ csrPem := pem.EncodeToMemory(&pem.Block{

@ -1,6 +1,7 @@
package main package main
import ( import (
"b612.me/apps/b612/aes"
"b612.me/apps/b612/attach" "b612.me/apps/b612/attach"
"b612.me/apps/b612/base64" "b612.me/apps/b612/base64"
"b612.me/apps/b612/base85" "b612.me/apps/b612/base85"
@ -48,7 +49,7 @@ func init() {
base64.Cmd, base85.Cmd, base91.Cmd, attach.Cmd, detach.Cmd, df.Cmd, dfinder.Cmd, base64.Cmd, base85.Cmd, base91.Cmd, attach.Cmd, detach.Cmd, df.Cmd, dfinder.Cmd,
ftp.Cmd, generate.Cmd, hash.Cmd, image.Cmd, merge.Cmd, search.Cmd, split.Cmd, vic.Cmd, ftp.Cmd, generate.Cmd, hash.Cmd, image.Cmd, merge.Cmd, search.Cmd, split.Cmd, vic.Cmd,
calc.Cmd, net.Cmd, rmt.Cmds, rmt.Cmdc, keygen.Cmd, dns.Cmd, whois.Cmd, socks5.Cmd, httproxy.Cmd, smtpserver.Cmd, smtpclient.Cmd, calc.Cmd, net.Cmd, rmt.Cmds, rmt.Cmdc, keygen.Cmd, dns.Cmd, whois.Cmd, socks5.Cmd, httproxy.Cmd, smtpserver.Cmd, smtpclient.Cmd,
cert.Cmd) cert.Cmd, aes.Cmd)
} }
func main() { func main() {

Loading…
Cancel
Save