Browse Source

add hash rand disable

joe 4 years ago
parent
commit
f8129c1b02
10 changed files with 226 additions and 7 deletions
  1. 1 1
      README.cn.md
  2. 1 1
      cnf/README.md
  3. 28 0
      oss/osssvr/disable.go
  4. 31 0
      sms/smsplat/disable_sms.go
  5. 1 1
      utl/common.go
  6. 34 0
      utl/hasher.go
  7. 29 0
      utl/hasher_test.go
  8. 43 0
      utl/rand.go
  9. 40 0
      utl/rand_test.go
  10. 18 4
      utl/smtp.go

+ 1 - 1
README.cn.md

@@ -13,7 +13,7 @@ Redistros of many useful libraries
 - [xorm]()
 - [gorm]()
 - [gopay]()
-- [viper]()
+- [viper](https://github.com/spf13/viper)
 - [flag]()
 - [cobra](https://github.com/spf13/cobra)
 

+ 1 - 1
cnf/README.md

@@ -1,4 +1,4 @@
 
 # No codes here
 
-Please use [viper]() directly, its interface is simple enough.
+Please use [viper](https://github.com/spf13/viper) directly, its interface is simple enough.

+ 28 - 0
oss/osssvr/disable.go

@@ -0,0 +1,28 @@
+package osssvr
+
+import (
+	"git.wanbits.io/joe/kettle/oss"
+	"io"
+)
+
+type disableOss struct{}
+
+func NewDisableOss() oss.IOss {
+	return &disableOss{}
+}
+
+func (self *disableOss) UploadFile(absPath string, key string, conf *oss.UploadConf) (string, error) {
+	return key, nil
+}
+
+func (self *disableOss) Upload(reader io.Reader, len int64, key string, conf *oss.UploadConf) (string, error) {
+	return key, nil
+}
+
+func (self *disableOss) Delete(key string) error {
+	return nil
+}
+
+func (self *disableOss) Exists(key string) bool {
+	return false
+}

+ 31 - 0
sms/smsplat/disable_sms.go

@@ -0,0 +1,31 @@
+package smsplat
+
+import (
+	"git.wanbits.io/joe/kettle/sms"
+)
+
+type disableSms struct{}
+
+func NewDisableSms() sms.ISms {
+	return &disableSms{}
+}
+
+func (self *disableSms) Send(phone, msg string) error {
+	return nil
+}
+
+func (self *disableSms) SendTpl(phone, tpl string, params []byte) error {
+	return nil
+}
+
+func (self *disableSms) GetBalance() (float64, error) {
+	return 0.0, nil
+}
+
+func (self *disableSms) GetAvailable() (int64, error) {
+	return 0, nil
+}
+
+func (self *disableSms) Name() string {
+	return "disableSms"
+}

+ 1 - 1
utl/common.go

@@ -6,7 +6,7 @@ import (
 )
 
 var (
-	ErrNotImplement       = errors.New("not implemented yet")
+	ErrNotImplemented       = errors.New("not implemented yet")
 	ErrInterfaceTransform = errors.New("interface transform failed")
 	ErrParameters         = errors.New("invalid parameters")
 	ErrContainerEmpty     = errors.New("target container is empty")

+ 34 - 0
utl/hasher.go

@@ -0,0 +1,34 @@
+package utl
+
+import (
+	"crypto/hmac"
+	"crypto/md5"
+	"crypto/sha1"
+	"crypto/sha256"
+	"encoding/hex"
+)
+
+var (
+	hmac256key = []byte("kettle-hmac-key@2021#gl")
+)
+
+func Md5(text string) string {
+	h := md5.Sum([]byte(text))
+	return hex.EncodeToString(h[:])
+}
+
+func Sha1(text string) string {
+	h := sha1.Sum([]byte(text))
+	return hex.EncodeToString(h[:])
+}
+
+func Sha256(text string) string {
+	h := sha256.Sum256([]byte(text))
+	return hex.EncodeToString(h[:])
+}
+
+func Hmac256(text string) string {
+	h := hmac.New(sha256.New, hmac256key)
+	h.Write([]byte(text))
+	return hex.EncodeToString(h.Sum(nil))
+}

+ 29 - 0
utl/hasher_test.go

@@ -0,0 +1,29 @@
+package utl
+
+import (
+	"strings"
+	"testing"
+)
+
+func TestMd5(t *testing.T) {
+	d := Md5("12345")
+	if d != strings.ToLower("827CCB0EEA8A706C4C34A16891F84E7B") {
+		t.Fatal("md5", d)
+	}
+}
+
+func TestSha1(t *testing.T) {
+	if Sha1("12345") != strings.ToLower("8CB2237D0679CA88DB6464EAC60DA96345513964") {
+		t.Fatal("sha1")
+	}
+}
+
+func TestSha256(t *testing.T) {
+	if Sha256("12345") != strings.ToLower("5994471ABB01112AFCC18159F6CC74B4F511B99806DA59B3CAF5A9C173CACFC5") {
+		t.Fatal("sha256")
+	}
+}
+
+func TestHmac256(t *testing.T) {
+
+}

+ 43 - 0
utl/rand.go

@@ -0,0 +1,43 @@
+package utl
+
+import (
+	"math/rand"
+	"time"
+)
+
+var (
+	s = rand.NewSource(time.Now().UnixNano())
+	r = rand.New(s)
+)
+
+func RandNumString(sz int) string {
+	b := make([]byte, sz)
+	for i := 0; i < sz; i++ {
+		b[i] = byte(0x30 + r.Intn(10))
+	}
+	return string(b)
+}
+
+func RandAlphaStr(sz int) string {
+	starts := []int{0x30, 0x41, 0x61}
+	lens := []int{10, 26, 26}
+	b := make([]byte, sz)
+	for i := 0; i < sz; i++ {
+		idx := r.Intn(len(starts))
+		b[i] = byte(starts[idx] + r.Intn(lens[idx]))
+	}
+	return string(b)
+}
+
+func RandInt(m, n int) int {
+	if m > n {
+		return RandInt(n, m)
+	}
+	return r.Intn(n-m) + m
+}
+
+var RandIntn = r.Intn
+
+func RandChoice(sarr []string) string {
+	return sarr[r.Intn(len(sarr))]
+}

+ 40 - 0
utl/rand_test.go

@@ -0,0 +1,40 @@
+package utl
+
+import (
+	"testing"
+)
+
+func TestRandInt(t *testing.T) {
+	var res int
+	for i := 0; i < 100; i++ {
+		res = RandInt(i, 101)
+		if res < i || res >= 101 {
+			t.Fatal("error res:", res)
+		}
+	}
+}
+
+func TestRandNumString(t *testing.T) {
+	s := RandNumString(6)
+	t.Log(s)
+}
+
+func TestRandAlphaStr(t *testing.T) {
+	s := RandAlphaStr(6)
+	t.Log(s)
+}
+
+func TestRandIntn(t *testing.T) {
+	var res int
+	for i := 1; i <= 100; i++ {
+		res = RandIntn(i)
+		if res >= i {
+			t.Fatal("error res:", res)
+		}
+	}
+}
+
+func TestRandChoice(t *testing.T) {
+	sarr := []string{"1", "2", "3", "4"}
+	t.Log(RandChoice(sarr))
+}

+ 18 - 4
utl/smtp.go

@@ -4,15 +4,19 @@ import (
 	"gopkg.in/gomail.v2"
 )
 
-type Smtp struct {
+type IMessager interface {
+	Send([]string, string, string) error
+}
+
+type smtp struct {
 	host               string
 	port               int
 	username, password string
 	alias              string
 }
 
-func NewSmtp(host string, port int, username, password, alias string) *Smtp {
-	return &Smtp{
+func NewSmtp(host string, port int, username, password, alias string) IMessager {
+	return &smtp{
 		host:     host,
 		port:     port,
 		username: username,
@@ -21,7 +25,7 @@ func NewSmtp(host string, port int, username, password, alias string) *Smtp {
 	}
 }
 
-func (self *Smtp) Send(to []string, subject, body string) error {
+func (self *smtp) Send(to []string, subject, body string) error {
 	m := gomail.NewMessage( /* gomail.SetEncoding(gomail.Base64) */ )
 	m.SetHeader("From", m.FormatAddress(self.username, self.alias))
 	m.SetHeader("To", to...)
@@ -38,3 +42,13 @@ func (self *Smtp) Send(to []string, subject, body string) error {
 	d := gomail.NewDialer(self.host, self.port, self.username, self.password)
 	return d.DialAndSend(m)
 }
+
+type disableMessager struct{}
+
+func NewDisableMessager() IMessager {
+	return &disableMessager{}
+}
+
+func (self *disableMessager) Send(to []string, subject, body string) error {
+	return nil
+}