From fd91f6808d53c932e2b4f7c81ecf7a6b427d6e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Fri, 16 Dec 2022 10:53:41 +0800 Subject: [PATCH] - update aes - update int64 - update decimal --- utils/goaes/{goaes.go => aes.go} | 0 utils/goaes/rsa.go | 58 ++++++++++++++++ utils/godecimal/big.go | 15 ++++ utils/godecimal/big_new.go | 29 ++++++++ utils/godecimal/big_operation.go | 113 ++++++++++++++++++++++++++++++ utils/godecimal/big_output.go | 81 ++++++++++++++++++++++ utils/godecimal/calculate.go | 21 ++++++ utils/godecimal/godecimal.go | 115 ------------------------------- utils/godecimal/gorm.go | 35 ++++++++++ utils/godecimal/math.go | 38 ++++++++++ utils/goint64/goint64.go | 10 ++- 11 files changed, 399 insertions(+), 116 deletions(-) rename utils/goaes/{goaes.go => aes.go} (100%) create mode 100644 utils/goaes/rsa.go create mode 100644 utils/godecimal/big.go create mode 100644 utils/godecimal/big_new.go create mode 100644 utils/godecimal/big_operation.go create mode 100644 utils/godecimal/big_output.go create mode 100644 utils/godecimal/calculate.go delete mode 100644 utils/godecimal/godecimal.go create mode 100644 utils/godecimal/gorm.go create mode 100644 utils/godecimal/math.go diff --git a/utils/goaes/goaes.go b/utils/goaes/aes.go similarity index 100% rename from utils/goaes/goaes.go rename to utils/goaes/aes.go diff --git a/utils/goaes/rsa.go b/utils/goaes/rsa.go new file mode 100644 index 00000000..d8dd9295 --- /dev/null +++ b/utils/goaes/rsa.go @@ -0,0 +1,58 @@ +package goaes + +import ( + "crypto" + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/base64" + "encoding/pem" + "errors" + "strings" +) + +func RsaSign(signContent string, privateKey string, hash crypto.Hash) string { + shaNew := hash.New() + shaNew.Write([]byte(signContent)) + hashed := shaNew.Sum(nil) + priKey, err := ParsePrivateKey(privateKey) + if err != nil { + panic(err) + } + + signature, err := rsa.SignPKCS1v15(rand.Reader, priKey, hash, hashed) + if err != nil { + panic(err) + } + return base64.StdEncoding.EncodeToString(signature) +} + +func ParsePrivateKey(privateKey string) (*rsa.PrivateKey, error) { + privateKey = FormatPrivateKey(privateKey) + // 2、解码私钥字节,生成加密对象 + block, _ := pem.Decode([]byte(privateKey)) + if block == nil { + return nil, errors.New("私钥信息错误") + } + // 3、解析DER编码的私钥,生成私钥对象 + priKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) + if err != nil { + return nil, err + } + return priKey, nil +} + +const ( + PEM_BEGIN = "-----BEGIN RSA PRIVATE KEY-----\n" + PEM_END = "\n-----END RSA PRIVATE KEY-----" +) + +func FormatPrivateKey(privateKey string) string { + if !strings.HasPrefix(privateKey, PEM_BEGIN) { + privateKey = PEM_BEGIN + privateKey + } + if !strings.HasSuffix(privateKey, PEM_END) { + privateKey = privateKey + PEM_END + } + return privateKey +} diff --git a/utils/godecimal/big.go b/utils/godecimal/big.go new file mode 100644 index 00000000..3a216473 --- /dev/null +++ b/utils/godecimal/big.go @@ -0,0 +1,15 @@ +package godecimal + +import ( + "math/big" +) + +type Decimal struct { + floatValue *big.Float +} + +func New() Decimal { + return Decimal{ + floatValue: new(big.Float), + } +} diff --git a/utils/godecimal/big_new.go b/utils/godecimal/big_new.go new file mode 100644 index 00000000..d9ed2348 --- /dev/null +++ b/utils/godecimal/big_new.go @@ -0,0 +1,29 @@ +package godecimal + +// NewString 从字符串创建 +func NewString(s string) Decimal { + d := New() + d.floatValue.SetString(s) + return d +} + +// NewFloat 从浮点数创建 +func NewFloat(f float64) Decimal { + d := New() + d.floatValue.SetFloat64(f) + return d +} + +// NewInt 从整数创建 +func NewInt(i int64) Decimal { + d := New() + d.floatValue.SetInt64(i) + return d +} + +// NewUint 从无符合整数创建 +func NewUint(i uint64) Decimal { + d := New() + d.floatValue.SetUint64(i) + return d +} diff --git a/utils/godecimal/big_operation.go b/utils/godecimal/big_operation.go new file mode 100644 index 00000000..3aa3f478 --- /dev/null +++ b/utils/godecimal/big_operation.go @@ -0,0 +1,113 @@ +package godecimal + +// Add 加 (d+d2) +func (d Decimal) Add(d2 Decimal) Decimal { + mul := New() + mul.floatValue.Add(d.floatValue, d2.floatValue) + return mul +} + +// AddFloat 加 +func (d Decimal) AddFloat(d2 float64) Decimal { + mul := New() + mul.floatValue.Add(d.floatValue, NewFloat(d2).floatValue) + return mul +} + +// AddInt 加 +func (d Decimal) AddInt(d2 int64) Decimal { + mul := New() + mul.floatValue.Add(d.floatValue, NewInt(d2).floatValue) + return mul +} + +// AddString 加 +func (d Decimal) AddString(d2 string) Decimal { + mul := New() + mul.floatValue.Add(d.floatValue, NewString(d2).floatValue) + return mul +} + +// Sub 减 (d-d2) +func (d Decimal) Sub(d2 Decimal) Decimal { + mul := New() + mul.floatValue.Sub(d.floatValue, d2.floatValue) + return mul +} + +// SubFloat 减 +func (d Decimal) SubFloat(d2 float64) Decimal { + mul := New() + mul.floatValue.Sub(d.floatValue, NewFloat(d2).floatValue) + return mul +} + +// SubInt 减 +func (d Decimal) SubInt(d2 int64) Decimal { + mul := New() + mul.floatValue.Sub(d.floatValue, NewInt(d2).floatValue) + return mul +} + +// SubString 减 +func (d Decimal) SubString(d2 string) Decimal { + mul := New() + mul.floatValue.Sub(d.floatValue, NewString(d2).floatValue) + return mul +} + +// Mul 乘 (d*d2) +func (d Decimal) Mul(d2 Decimal) Decimal { + mul := New() + mul.floatValue.Mul(d.floatValue, d2.floatValue) + return mul +} + +// MulFloat 乘 +func (d Decimal) MulFloat(d2 float64) Decimal { + mul := New() + mul.floatValue.Mul(d.floatValue, NewFloat(d2).floatValue) + return mul +} + +// MulInt 乘 +func (d Decimal) MulInt(d2 int64) Decimal { + mul := New() + mul.floatValue.Mul(d.floatValue, NewInt(d2).floatValue) + return mul +} + +// MulString 乘 +func (d Decimal) MulString(d2 string) Decimal { + mul := New() + mul.floatValue.Mul(d.floatValue, NewString(d2).floatValue) + return mul +} + +// Quo 除 (d/d2) +func (d Decimal) Quo(d2 Decimal) Decimal { + mul := New() + mul.floatValue.Quo(d.floatValue, d2.floatValue) + return mul +} + +// QuoFloat 除 +func (d Decimal) QuoFloat(d2 float64) Decimal { + mul := New() + mul.floatValue.Quo(d.floatValue, NewFloat(d2).floatValue) + return mul +} + +// QuoInt 除 +func (d Decimal) QuoInt(d2 int64) Decimal { + mul := New() + mul.floatValue.Quo(d.floatValue, NewInt(d2).floatValue) + return mul +} + +// QuoString 除 +func (d Decimal) QuoString(d2 string) Decimal { + mul := New() + mul.floatValue.Quo(d.floatValue, NewString(d2).floatValue) + return mul +} diff --git a/utils/godecimal/big_output.go b/utils/godecimal/big_output.go new file mode 100644 index 00000000..e5725f6f --- /dev/null +++ b/utils/godecimal/big_output.go @@ -0,0 +1,81 @@ +package godecimal + +import ( + "fmt" + "math" + "math/big" + "strings" +) + +// String 输出 string +func (d Decimal) String() string { + return d.floatValue.String() +} + +// Int64 输出 int64 +func (d Decimal) Int64() int64 { + i64, _ := d.floatValue.Int64() + return i64 +} + +// Float64 输出 float64 +func (d Decimal) Float64() float64 { + rat, _ := new(big.Rat).SetString(d.String()) + f, _ := rat.Float64() + return f +} + +// MoneyFloat64 货币 float64 +func (d Decimal) MoneyFloat64() float64 { + rat, _ := new(big.Rat).SetString(d.floatValue.Text('f', 2)) + f, _ := rat.Float64() + return f +} + +// Float64Point 输出float64带小数点 +func (d Decimal) Float64Point(p int) float64 { + rat, _ := new(big.Rat).SetString(d.floatValue.Text('f', p)) + f, _ := rat.Float64() + return f +} + +// Float64PointAdaptive 输出float64带小数点(自适应) +func (d Decimal) Float64PointAdaptive(maxP int) float64 { + f, _ := d.floatValue.Float64() + if maxP > 0 { + pL := d.pointLength(f) + if pL > maxP { + rat, _ := new(big.Rat).SetString(d.floatValue.Text('f', maxP)) + f2, _ := rat.Float64() + return f2 + } else { + return f + } + } else { + return f + } +} + +func (Decimal) pointLength(a any) int { + tmp := strings.Split(fmt.Sprint(a), ".") + if len(tmp) <= 1 { + return 0 + } + return len(tmp[1]) +} + +// IsInteger 是否为整数 +func (d Decimal) IsInteger(d2 float64) bool { + if d2 > 0 { + f3 := NewFloat(d.Float64()).QuoFloat(NewFloat(d2).Float64()).Float64() + if f3 == math.Trunc(f3) { + return true + } + return false + } + f3 := NewFloat(d.Float64()).Float64() + if f3 == math.Trunc(f3) { + return true + } + return false +} diff --git a/utils/godecimal/calculate.go b/utils/godecimal/calculate.go new file mode 100644 index 00000000..51abbf61 --- /dev/null +++ b/utils/godecimal/calculate.go @@ -0,0 +1,21 @@ +package godecimal + +// Float64Add 加 (f1+f2) +func Float64Add(f1, f2 float64) float64 { + return NewFloat(f1).Add(NewFloat(f2)).Float64() +} + +// Float64Sub 减 (f1-f2) +func Float64Sub(f1, f2 float64) float64 { + return NewFloat(f1).Sub(NewFloat(f2)).Float64() +} + +// Float64Mul 乘 (f1*f2) +func Float64Mul(f1, f2 float64) float64 { + return NewFloat(f1).Mul(NewFloat(f2)).Float64() +} + +// Float64Quo 除 (f1/f2) +func Float64Quo(f1, f2 float64) float64 { + return NewFloat(f1).Quo(NewFloat(f2)).Float64() +} diff --git a/utils/godecimal/godecimal.go b/utils/godecimal/godecimal.go deleted file mode 100644 index 0c97638b..00000000 --- a/utils/godecimal/godecimal.go +++ /dev/null @@ -1,115 +0,0 @@ -package godecimal - -import ( - "fmt" - "github.com/dtapps/go-library/utils/gostring" - "math" - "strconv" -) - -// Decimal 四舍五入 -func Decimal(value float64) float64 { - value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64) - return value -} - -// Round 四舍五入 -func Round(f float64, n int) float64 { - n10 := math.Pow10(n) - return math.Trunc((f+0.5/n10)*n10) / n10 -} - -// RoundYInt64 四舍五入 -func RoundYInt64(y int64, n int) float64 { - return Round(float64(y/100), n) -} - -// RoundYString 四舍五入 -func RoundYString(y string, n int) float64 { - return Round(gostring.ToFloat64(y)/100, n) -} - -// Multiply 相乘 -func Multiply(y, x float64) float64 { - return Round(y*x, 2) -} - -// PddCouponAmount 优惠券金额 -func PddCouponAmount(y int64) float64 { - return Round(float64(y)/100, 2) -} - -// PddCouponProportion 拼多多佣金比率 -func PddCouponProportion(y int64) float64 { - return Round(float64(y)/10, 2) -} - -// PddGoodsOriginalPrice 拼多多商品原价 -func PddGoodsOriginalPrice(y int64) float64 { - return Round(float64(y)/100, 2) -} - -// PddGoodsPrice 拼多多商品券后价 -func PddGoodsPrice(y, x float64) float64 { - return Round(y-x, 2) -} - -// PddCommission 拼多多佣金 -func PddCommission(y, x float64) float64 { - return Round((y*x)/100, 2) -} - -// TbCouponAmount 淘宝优惠券金额 -func TbCouponAmount(y int64) float64 { - return Round(float64(y), 2) -} - -// TbCouponProportion 淘宝佣金比率 -func TbCouponProportion(y string) float64 { - return Round(gostring.ToFloat64(y)/100, 2) -} - -// TbGoodsOriginalPrice 淘宝商品原价 -func TbGoodsOriginalPrice(y string) float64 { - return Round(gostring.ToFloat64(y), 2) -} - -// TbGoodsPrice 淘宝商品券后价 -func TbGoodsPrice(y, x float64) float64 { - return Round(y-x, 2) -} - -// TbCommission 淘宝佣金 -func TbCommission(y, x float64) float64 { - return Round((y*x)/100, 2) -} - -// WmCouponAmount 小商店优惠券金额 -func WmCouponAmount(y string) float64 { - return Round(gostring.ToFloat64(y)/100, 2) -} - -// WmCouponProportion 小商店佣金比率 -func WmCouponProportion(y int64) float64 { - return Round(float64(y)/100, 2) -} - -// WmCommission 小商店佣金 -func WmCommission(y int64) float64 { - return Round(float64(y)/100, 2) -} - -// WmGoodsOriginalPrice 小商店商品原价 -func WmGoodsOriginalPrice(y int64) float64 { - return Round(float64(y)/100, 2) -} - -// WmGoodsPrice 小商店商品券后价 -func WmGoodsPrice(y int64) float64 { - return Round(float64(y)/100, 2) -} - -// JdCommission 京东佣金 -func JdCommission(y, x float64) float64 { - return Round((y*x)/100, 2) -} diff --git a/utils/godecimal/gorm.go b/utils/godecimal/gorm.go new file mode 100644 index 00000000..5ea89a8a --- /dev/null +++ b/utils/godecimal/gorm.go @@ -0,0 +1,35 @@ +package godecimal + +import ( + "database/sql/driver" + "fmt" + "strings" +) + +type DFloat struct { + FValue float64 + Point int +} + +// Value 实现 driver.Valuer 接口,Value 返回 json value +func (f DFloat) Value() (driver.Value, error) { + return NewFloat(f.FValue).Float64PointAdaptive(f.Point), nil +} + +// Scan 方法实现了 sql.Scanner 接口 +func (f *DFloat) Scan(value interface{}) error { + f1, _ := value.(float64) + *f = DFloat{ + FValue: f1, + Point: pointLength(f1), + } + return nil +} + +func pointLength(a any) int { + tmp := strings.Split(fmt.Sprint(a), ".") + if len(tmp) <= 1 { + return 0 + } + return len(tmp[1]) +} diff --git a/utils/godecimal/math.go b/utils/godecimal/math.go new file mode 100644 index 00000000..7a3ac0b5 --- /dev/null +++ b/utils/godecimal/math.go @@ -0,0 +1,38 @@ +package godecimal + +import "math" + +// Abs 取绝对值 +func Abs(x float64) float64 { + return math.Abs(x) +} + +// Floor 向下取整 +func Floor(x float64) float64 { + return math.Floor(x) +} + +// Ceil 向上取整 +func Ceil(x float64) float64 { + return math.Ceil(x) +} + +// Round 就近取整 +func Round(x float64) float64 { + return math.Round(x) +} + +// RoundPoint 就近取整并保留小数点 +func RoundPoint(x float64) float64 { + return math.Round(x*100) / 100 +} + +// Max 取较大值 +func Max(x, y float64) float64 { + return math.Max(x, y) +} + +// Min 取较小值 +func Min(x, y float64) float64 { + return math.Min(x, y) +} diff --git a/utils/goint64/goint64.go b/utils/goint64/goint64.go index 20fe1aac..dfee53e4 100644 --- a/utils/goint64/goint64.go +++ b/utils/goint64/goint64.go @@ -1,6 +1,9 @@ package goint64 -import "math" +import ( + "math" + "strconv" +) // ToFloat64 int64到float64 func ToFloat64(n int64) float64 { @@ -21,3 +24,8 @@ func ToUnwrapToInt64(num int64, retain int) int64 { func ToFloat64NewWiFi(num int64) float64 { return float64(num / 100) } + +// ToString int到string +func ToString(n int64) string { + return strconv.FormatInt(n, 10) +}