|
|
|
@ -29,6 +29,7 @@ import (
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// StarCrypto 存储了单个校验方法的非编码元数据
|
|
|
|
|
type StarCrypto struct {
|
|
|
|
|
Sha1 []byte
|
|
|
|
|
Sha224 []byte
|
|
|
|
@ -39,74 +40,58 @@ type StarCrypto struct {
|
|
|
|
|
Md5 []byte
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
输出格式化后的Base64字符串
|
|
|
|
|
*/
|
|
|
|
|
func (this StarCrypto) Base64Encode(bstr []byte) string {
|
|
|
|
|
// Base64Encode 输出格式化后的Base64字符串
|
|
|
|
|
func (starcpto StarCrypto) Base64Encode(bstr []byte) string {
|
|
|
|
|
return base64.StdEncoding.EncodeToString(bstr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
输出解密前的Base64数据
|
|
|
|
|
*/
|
|
|
|
|
func (this StarCrypto) Base64Decode(str string) ([]byte, error) {
|
|
|
|
|
// Base64Decode 输出解密前的Base64数据
|
|
|
|
|
func (starcpto StarCrypto) Base64Decode(str string) ([]byte, error) {
|
|
|
|
|
return base64.StdEncoding.DecodeString(str)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
输出MD5校验值
|
|
|
|
|
*/
|
|
|
|
|
func (this *StarCrypto) MD5(bstr []byte) string {
|
|
|
|
|
// MD5 输出MD5校验值
|
|
|
|
|
func (starcpto *StarCrypto) MD5(bstr []byte) string {
|
|
|
|
|
md5sum := md5.New()
|
|
|
|
|
md5sum.Write(bstr)
|
|
|
|
|
this.Md5 = md5sum.Sum(nil)
|
|
|
|
|
return hex.EncodeToString(this.Md5)
|
|
|
|
|
starcpto.Md5 = md5sum.Sum(nil)
|
|
|
|
|
return hex.EncodeToString(starcpto.Md5)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
输出CRC32校验值
|
|
|
|
|
*/
|
|
|
|
|
func (this *StarCrypto) CRC32(bstr []byte) string {
|
|
|
|
|
// CRC32 输出CRC32校验值
|
|
|
|
|
func (starcpto *StarCrypto) CRC32(bstr []byte) string {
|
|
|
|
|
crcsum := crc32.NewIEEE()
|
|
|
|
|
crcsum.Write(bstr)
|
|
|
|
|
this.Crc32 = crcsum.Sum(nil)
|
|
|
|
|
return hex.EncodeToString(this.Crc32)
|
|
|
|
|
starcpto.Crc32 = crcsum.Sum(nil)
|
|
|
|
|
return hex.EncodeToString(starcpto.Crc32)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
输出SHA512校验值
|
|
|
|
|
*/
|
|
|
|
|
func (this *StarCrypto) SHA512(bstr []byte) string {
|
|
|
|
|
// SHA512 输出SHA512校验值
|
|
|
|
|
func (starcpto *StarCrypto) SHA512(bstr []byte) string {
|
|
|
|
|
shasum := sha512.New()
|
|
|
|
|
shasum.Write(bstr)
|
|
|
|
|
this.Sha512 = shasum.Sum(nil)
|
|
|
|
|
return hex.EncodeToString(this.Sha512)
|
|
|
|
|
starcpto.Sha512 = shasum.Sum(nil)
|
|
|
|
|
return hex.EncodeToString(starcpto.Sha512)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
输出SHA256校验值
|
|
|
|
|
*/
|
|
|
|
|
func (this *StarCrypto) SHA256(bstr []byte) string {
|
|
|
|
|
// SHA256 输出SHA256校验值
|
|
|
|
|
func (starcpto *StarCrypto) SHA256(bstr []byte) string {
|
|
|
|
|
shasum := sha256.New()
|
|
|
|
|
shasum.Write(bstr)
|
|
|
|
|
this.Sha256 = shasum.Sum(nil)
|
|
|
|
|
return hex.EncodeToString(this.Sha256)
|
|
|
|
|
starcpto.Sha256 = shasum.Sum(nil)
|
|
|
|
|
return hex.EncodeToString(starcpto.Sha256)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
输出SHA1校验值
|
|
|
|
|
*/
|
|
|
|
|
func (this *StarCrypto) SHA1(bstr []byte) string {
|
|
|
|
|
// SHA1 输出SHA1校验值
|
|
|
|
|
func (starcpto *StarCrypto) SHA1(bstr []byte) string {
|
|
|
|
|
shasum := sha1.New()
|
|
|
|
|
shasum.Write(bstr)
|
|
|
|
|
this.Sha1 = shasum.Sum(nil)
|
|
|
|
|
return hex.EncodeToString(this.Sha1)
|
|
|
|
|
starcpto.Sha1 = shasum.Sum(nil)
|
|
|
|
|
return hex.EncodeToString(starcpto.Sha1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
多校验方法校验函数
|
|
|
|
|
*/
|
|
|
|
|
func (this StarCrypto) SumAll(data []byte, method []string) (map[string]string, error) {
|
|
|
|
|
// SumAll 可以对同一数据进行多种校验
|
|
|
|
|
func (starcpto StarCrypto) SumAll(data []byte, method []string) (map[string]string, error) {
|
|
|
|
|
result := make(map[string]string)
|
|
|
|
|
methods := make(map[string]hash.Hash)
|
|
|
|
|
var iscrc bool
|
|
|
|
@ -154,17 +139,15 @@ func (this StarCrypto) SumAll(data []byte, method []string) (map[string]string,
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
输出文件内容校验值,method为单个校验方法,小写
|
|
|
|
|
例:FileSum("./test.txt","md5",shell(pect float64){fmt.Sprintf("已完成 %f\r",pect)})
|
|
|
|
|
*/
|
|
|
|
|
func (this StarCrypto) FileSum(filepath, method string, shell func(float64)) (string, error) {
|
|
|
|
|
// FileSum 输出文件内容校验值,method为单个校验方法,小写
|
|
|
|
|
//例:FileSum("./test.txt","md5",shell(pect float64){fmt.Sprintf("已完成 %f\r",pect)})
|
|
|
|
|
func (starcpto StarCrypto) FileSum(filepath, method string, shell func(float64)) (string, error) {
|
|
|
|
|
var sum hash.Hash
|
|
|
|
|
var sum32 hash.Hash32
|
|
|
|
|
var issum32 bool
|
|
|
|
|
var result string
|
|
|
|
|
if !Exists(filepath) {
|
|
|
|
|
return "", errors.New("File Not Exists!")
|
|
|
|
|
return "", errors.New("file not exists")
|
|
|
|
|
}
|
|
|
|
|
fp, err := os.Open(filepath)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -229,10 +212,8 @@ func (this StarCrypto) FileSum(filepath, method string, shell func(float64)) (st
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
多校验方法校验文件函数
|
|
|
|
|
*/
|
|
|
|
|
func (this StarCrypto) FileSumAll(filepath string, method []string, shell func(float64)) (map[string]string, error) {
|
|
|
|
|
// FileSumAll 可以对同一文件进行多种校验
|
|
|
|
|
func (starcpto StarCrypto) FileSumAll(filepath string, method []string, shell func(float64)) (map[string]string, error) {
|
|
|
|
|
result := make(map[string]string)
|
|
|
|
|
methods := make(map[string]hash.Hash)
|
|
|
|
|
var iscrc bool
|
|
|
|
@ -241,7 +222,7 @@ func (this StarCrypto) FileSumAll(filepath string, method []string, shell func(f
|
|
|
|
|
method = []string{"sha512", "sha256", "sha384", "sha224", "sha1", "crc32", "md5"}
|
|
|
|
|
}
|
|
|
|
|
if !Exists(filepath) {
|
|
|
|
|
return result, errors.New("File Not Exists!")
|
|
|
|
|
return result, errors.New("file not exists")
|
|
|
|
|
}
|
|
|
|
|
fp, err := os.Open(filepath)
|
|
|
|
|
defer fp.Close()
|
|
|
|
@ -305,12 +286,13 @@ func (this StarCrypto) FileSumAll(filepath string, method []string, shell func(f
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this StarCrypto) Attach(src, dst, output string) error {
|
|
|
|
|
// Attach 合并src与dst文件并输出到output中
|
|
|
|
|
func (starcpto StarCrypto) Attach(src, dst, output string) error {
|
|
|
|
|
if !Exists(src) {
|
|
|
|
|
return errors.New("Source File Not Exists!")
|
|
|
|
|
return errors.New("source file not exists")
|
|
|
|
|
}
|
|
|
|
|
if !Exists(dst) {
|
|
|
|
|
return errors.New("Dest File Not Exists!")
|
|
|
|
|
return errors.New("dst file not exists")
|
|
|
|
|
}
|
|
|
|
|
fpsrc, err := os.Open(src)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -344,9 +326,10 @@ func (this StarCrypto) Attach(src, dst, output string) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this StarCrypto) Detach(src string, bytenum int, dst1, dst2 string) error {
|
|
|
|
|
// Detach 按bytenum字节大小分割src文件到dst1与dst2两个新文件中去
|
|
|
|
|
func (starcpto StarCrypto) Detach(src string, bytenum int, dst1, dst2 string) error {
|
|
|
|
|
if !Exists(src) {
|
|
|
|
|
return errors.New("Source File Not Exists!")
|
|
|
|
|
return errors.New("source file not exists")
|
|
|
|
|
}
|
|
|
|
|
fpsrc, err := os.Open(src)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -395,9 +378,10 @@ func (this StarCrypto) Detach(src string, bytenum int, dst1, dst2 string) error
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this StarCrypto) Base64EncodeFile(src, dst string, shell func(float64)) error {
|
|
|
|
|
// Base64EncodeFile 用base64方法编码src文件到dst文件中去,shell传入当前进度
|
|
|
|
|
func (starcpto StarCrypto) Base64EncodeFile(src, dst string, shell func(float64)) error {
|
|
|
|
|
if !Exists(src) {
|
|
|
|
|
return errors.New("Source File Not Exists!")
|
|
|
|
|
return errors.New("source file not exists")
|
|
|
|
|
}
|
|
|
|
|
fpsrc, err := os.Open(src)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -429,9 +413,10 @@ func (this StarCrypto) Base64EncodeFile(src, dst string, shell func(float64)) er
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this StarCrypto) Base64DecodeFile(src, dst string, shell func(float64)) error {
|
|
|
|
|
// Base64DecodeFile 用base64方法解码src文件到dst文件中去,shell传入当前进度
|
|
|
|
|
func (starcpto StarCrypto) Base64DecodeFile(src, dst string, shell func(float64)) error {
|
|
|
|
|
if !Exists(src) {
|
|
|
|
|
return errors.New("Source File Not Exists!")
|
|
|
|
|
return errors.New("source file not exists")
|
|
|
|
|
}
|
|
|
|
|
fpsrc, err := os.Open(src)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -464,13 +449,12 @@ func (this StarCrypto) Base64DecodeFile(src, dst string, shell func(float64)) er
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
如果bynum=true 则把文件分割成num份
|
|
|
|
|
如果bynum=false 则把文件按num字节分成多份
|
|
|
|
|
*/
|
|
|
|
|
func (this StarCrypto) SplitFile(src, dst string, num int, bynum bool, shell func(float64)) error {
|
|
|
|
|
// SplitFile 把src文件按要求分割到dst中去,dst应传入带*号字符串
|
|
|
|
|
// 如果bynum=true 则把文件分割成num份
|
|
|
|
|
// 如果bynum=false 则把文件按num字节分成多份
|
|
|
|
|
func (starcpto StarCrypto) SplitFile(src, dst string, num int, bynum bool, shell func(float64)) error {
|
|
|
|
|
if !Exists(src) {
|
|
|
|
|
return errors.New("Source File Not Exists!")
|
|
|
|
|
return errors.New("source file not exists")
|
|
|
|
|
}
|
|
|
|
|
fpsrc, err := os.Open(src)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -481,7 +465,7 @@ func (this StarCrypto) SplitFile(src, dst string, num int, bynum bool, shell fun
|
|
|
|
|
filebig := float64(stat.Size())
|
|
|
|
|
if bynum {
|
|
|
|
|
if int(filebig) < num {
|
|
|
|
|
return errors.New("File is too small to split")
|
|
|
|
|
return errors.New("file is too small to split")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
balance := int(filebig/float64(num)) + 1
|
|
|
|
@ -526,7 +510,8 @@ func (this StarCrypto) SplitFile(src, dst string, num int, bynum bool, shell fun
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this StarCrypto) MergeFile(src, dst string, shell func(float64)) error {
|
|
|
|
|
// MergeFile 合并src文件到dst文件中去,src文件应传入带*号字符串
|
|
|
|
|
func (starcpto StarCrypto) MergeFile(src, dst string, shell func(float64)) error {
|
|
|
|
|
tmp := strings.Replace(src, "*", "0", -1)
|
|
|
|
|
dir, err := ioutil.ReadDir(filepath.Dir(tmp))
|
|
|
|
|
if err != nil {
|
|
|
|
@ -536,7 +521,7 @@ func (this StarCrypto) MergeFile(src, dst string, shell func(float64)) error {
|
|
|
|
|
tmp = strings.Replace(base, "*", "(\\d+)", -1)
|
|
|
|
|
reg := regexp.MustCompile(tmp)
|
|
|
|
|
count := 0
|
|
|
|
|
var filebig float64 = 0
|
|
|
|
|
var filebig float64
|
|
|
|
|
for _, v := range dir {
|
|
|
|
|
if reg.MatchString(v.Name()) {
|
|
|
|
|
count++
|
|
|
|
@ -572,10 +557,11 @@ func (this StarCrypto) MergeFile(src, dst string, shell func(float64)) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this StarCrypto) RSAEncrypt(data, public []byte) ([]byte, error) {
|
|
|
|
|
// RSAEncrypt RSA公钥加密
|
|
|
|
|
func (starcpto StarCrypto) RSAEncrypt(data, public []byte) ([]byte, error) {
|
|
|
|
|
blk, _ := pem.Decode(public)
|
|
|
|
|
if blk == nil {
|
|
|
|
|
return []byte{}, errors.New("public key error!")
|
|
|
|
|
return []byte{}, errors.New("public key error")
|
|
|
|
|
}
|
|
|
|
|
pubkey, err := x509.ParsePKIXPublicKey(blk.Bytes)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -584,7 +570,8 @@ func (this StarCrypto) RSAEncrypt(data, public []byte) ([]byte, error) {
|
|
|
|
|
return rsa.EncryptPKCS1v15(rand.Reader, pubkey.(*rsa.PublicKey), data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this StarCrypto) RSADecrypt(data, private []byte, password string) ([]byte, error) {
|
|
|
|
|
// RSADecrypt RSA私钥解密
|
|
|
|
|
func (starcpto StarCrypto) RSADecrypt(data, private []byte, password string) ([]byte, error) {
|
|
|
|
|
var prikey *rsa.PrivateKey
|
|
|
|
|
var err error
|
|
|
|
|
var bytes []byte
|
|
|
|
@ -612,7 +599,8 @@ func (this StarCrypto) RSADecrypt(data, private []byte, password string) ([]byte
|
|
|
|
|
return rsa.DecryptPKCS1v15(rand.Reader, prikey, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this StarCrypto) RSASign(hashdata, private []byte, password string, hashtype crypto.Hash) ([]byte, error) {
|
|
|
|
|
// RSASign RSA私钥签名加密
|
|
|
|
|
func (starcpto StarCrypto) RSASign(hashdata, private []byte, password string, hashtype crypto.Hash) ([]byte, error) {
|
|
|
|
|
var prikey *rsa.PrivateKey
|
|
|
|
|
var err error
|
|
|
|
|
var bytes []byte
|
|
|
|
@ -640,7 +628,8 @@ func (this StarCrypto) RSASign(hashdata, private []byte, password string, hashty
|
|
|
|
|
return rsa.SignPKCS1v15(rand.Reader, prikey, hashtype, hashdata)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this StarCrypto) RSAVerify(data, hashdata, public []byte, hashtype crypto.Hash) error {
|
|
|
|
|
// RSAVerify RSA公钥签名验证
|
|
|
|
|
func (starcpto StarCrypto) RSAVerify(data, hashdata, public []byte, hashtype crypto.Hash) error {
|
|
|
|
|
blk, _ := pem.Decode(public)
|
|
|
|
|
if blk == nil {
|
|
|
|
|
return errors.New("public key error!")
|
|
|
|
@ -652,7 +641,8 @@ func (this StarCrypto) RSAVerify(data, hashdata, public []byte, hashtype crypto.
|
|
|
|
|
return rsa.VerifyPKCS1v15(pubkey.(*rsa.PublicKey), hashtype, hashdata, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this StarCrypto) VicqueEncodeV1(srcdata []byte, key string) []byte {
|
|
|
|
|
// VicqueEncodeV1 Best!
|
|
|
|
|
func (starcpto StarCrypto) VicqueEncodeV1(srcdata []byte, key string) []byte {
|
|
|
|
|
var keys []int
|
|
|
|
|
var saku, piku uint8
|
|
|
|
|
data := make([]byte, len(srcdata))
|
|
|
|
@ -699,7 +689,8 @@ func (this StarCrypto) VicqueEncodeV1(srcdata []byte, key string) []byte {
|
|
|
|
|
return data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this StarCrypto) VicqueDecodeV1(srcdata []byte, key string) []byte {
|
|
|
|
|
// VicqueDecodeV1 Best!
|
|
|
|
|
func (starcpto StarCrypto) VicqueDecodeV1(srcdata []byte, key string) []byte {
|
|
|
|
|
var keys []int
|
|
|
|
|
var saku, piku int
|
|
|
|
|
data := make([]byte, len(srcdata))
|
|
|
|
@ -746,7 +737,8 @@ func (this StarCrypto) VicqueDecodeV1(srcdata []byte, key string) []byte {
|
|
|
|
|
return data[:lens]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this StarCrypto) VicqueEncodeV1File(src, dst, pwd string, shell func(float64)) error {
|
|
|
|
|
// VicqueEncodeV1File best
|
|
|
|
|
func (starcpto StarCrypto) VicqueEncodeV1File(src, dst, pwd string, shell func(float64)) error {
|
|
|
|
|
fpsrc, err := os.Open(src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
@ -772,13 +764,14 @@ func (this StarCrypto) VicqueEncodeV1File(src, dst, pwd string, shell func(float
|
|
|
|
|
}
|
|
|
|
|
sum += n
|
|
|
|
|
go shell(float64(sum) / filebig * 100)
|
|
|
|
|
data := this.VicqueEncodeV1(buf[0:n], pwd)
|
|
|
|
|
data := starcpto.VicqueEncodeV1(buf[0:n], pwd)
|
|
|
|
|
fpdst.Write(data)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this StarCrypto) VicqueDecodeV1File(src, dst, pwd string, shell func(float64)) error {
|
|
|
|
|
// VicqueDecodeV1File best
|
|
|
|
|
func (starcpto StarCrypto) VicqueDecodeV1File(src, dst, pwd string, shell func(float64)) error {
|
|
|
|
|
fpsrc, err := os.Open(src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
@ -804,12 +797,13 @@ func (this StarCrypto) VicqueDecodeV1File(src, dst, pwd string, shell func(float
|
|
|
|
|
}
|
|
|
|
|
sum += n
|
|
|
|
|
go shell(float64(sum) / filebig * 100)
|
|
|
|
|
data := this.VicqueDecodeV1(buf[0:n], pwd)
|
|
|
|
|
data := starcpto.VicqueDecodeV1(buf[0:n], pwd)
|
|
|
|
|
fpdst.Write(data)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FillWithRandom 随机写filesize大小的文件,每次buf大小为bufcap,随机bufnum个字符
|
|
|
|
|
func FillWithRandom(filepath string, filesize int, bufcap int, bufnum int, shell func(float64)) error {
|
|
|
|
|
var buf [][]byte
|
|
|
|
|
var buftmp []byte
|
|
|
|
|