Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
doodle committed May 24, 2022
0 parents commit d7fca2e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
11 changes: 11 additions & 0 deletions common/cryptx/crypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cryptx

import (
"fmt"
"golang.org/x/crypto/scrypt"
)

func PasswordEncrypt (salt, password string) string {
dk, _ := scrypt.Key([]byte(password), []byte(salt), 32768, 8, 1, 32)
return fmt.Sprintf("%x", string(dk))
}
13 changes: 13 additions & 0 deletions common/cryptx/crypt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cryptx

import (
"fmt"
"testing"
)

func TestPasswordEncrypt(t *testing.T) {
salt := "HWVOFkGgPTryzICwd7qnJaZR9KQ2i8xe"
password := "123456"
encrypt := PasswordEncrypt(salt, password)
fmt.Println(encrypt)
}
32 changes: 32 additions & 0 deletions common/errorx/baseerror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package errorx

const defaultCode = 1001

type CodeError struct {
Code int `json:"code"`
Msg string `json:"msg"`
}

type CodeErrorResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
}

func NewCodeError(code int, msg string) error {
return &CodeError{Code: code, Msg: msg}
}

func NewDefaultError(msg string) error {
return NewCodeError(defaultCode, msg)
}

func (e *CodeError) Error() string {
return e.Msg
}

func (e *CodeError) Data() *CodeErrorResponse {
return &CodeErrorResponse{
Code: e.Code,
Msg: e.Msg,
}
}
19 changes: 19 additions & 0 deletions common/res/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package res

type Body struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data,omitempty"`
}

func Response (resp interface{}, err error) Body {
var body Body
if err != nil {
body.Code = -1
body.Msg = err.Error()
} else {
body.Msg = "OK"
body.Data = resp
}
return body
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/doodle971002/stools

go 1.18

require golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898 h1:SLP7Q4Di66FONjDJbCYrCRrh97focO6sLogHO7/g8F0=
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=

0 comments on commit d7fca2e

Please sign in to comment.