Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add two buildin function ( decode and encode) #7622

Merged
merged 20 commits into from
Sep 10, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add comment to functions
  • Loading branch information
lanjingquan committed Sep 6, 2018
commit a1af03548fb9b77cb4d001b7845f77375919128d
21 changes: 12 additions & 9 deletions util/encrypt/crypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 24,9 @@ type randStruct struct {
maxValueDbl float64
}

// randomInit random generation structure initialization
func (rs *randStruct) randomInit(password []byte, length int) {
// hash password
// Generate binary hash from raw text string
var nr, add, nr2, tmp uint32
nr = 1345345333
add = 7
Expand All @@ -48,7 49,7 @@ func (rs *randStruct) randomInit(password []byte, length int) {
fmt.Println(seed1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the debug code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done! Thanks!

fmt.Println(seed2)

// init rand struct
// New (MySQL 3.21 ) random generation structure initialization
rs.maxValue = 0x3FFFFFFF
rs.maxValueDbl = float64(rs.maxValue)
rs.seed1 = seed1 % rs.maxValue
Expand All @@ -62,7 63,7 @@ func (rs *randStruct) myRand() float64 {
return ((float64(rs.seed1)) / rs.maxValueDbl)
}

type SqlCrypt struct {
type SQLCrypt struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd better not export struct SQLCrypt if it's not used outside module util/crypt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

rand randStruct
orgRand randStruct

Expand All @@ -71,7 72,7 @@ type SqlCrypt struct {
shift uint32
}

func (sc *SqlCrypt) init(password []byte, length int) {
func (sc *SQLCrypt) init(password []byte, length int) {
sc.rand.randomInit(password, length)

for i := 0; i <= 255; i {
Expand All @@ -93,12 94,12 @@ func (sc *SqlCrypt) init(password []byte, length int) {
sc.shift = 0
}

func (sc *SqlCrypt) reinit() {
func (sc *SQLCrypt) reinit() {
sc.shift = 0
sc.rand = sc.orgRand
}

func (sc *SqlCrypt) encode(str []byte, length int) {
func (sc *SQLCrypt) encode(str []byte, length int) {
for i := 0; i < length; i {
sc.shift ^= uint32(sc.rand.myRand() * 255.0)
idx := uint32(str[i])
Expand All @@ -107,7 108,7 @@ func (sc *SqlCrypt) encode(str []byte, length int) {
}
}

func (sc *SqlCrypt) decode(str []byte, length int) {
func (sc *SQLCrypt) decode(str []byte, length int) {
for i := 0; i < length; i {
sc.shift ^= uint32(sc.rand.myRand() * 255.0)
idx := uint32(str[i] ^ byte(sc.shift))
Expand All @@ -116,8 117,9 @@ func (sc *SqlCrypt) decode(str []byte, length int) {
}
}

//SQLDecode Function to handle the decode() function
func SQLDecode(str string, password string) (string, error) {
var sqlCrypt SqlCrypt
var sqlCrypt SQLCrypt

strByte := []byte(str)
passwdByte := []byte(password)
Expand All @@ -128,8 130,9 @@ func SQLDecode(str string, password string) (string, error) {
return string(strByte), nil
}

// SQLEncode Function to handle the encode() function
func SQLEncode(cryptStr string, password string) (string, error) {
var sqlCrypt SqlCrypt
var sqlCrypt SQLCrypt

cryptStrByte := []byte(cryptStr)
passwdByte := []byte(password)
Expand Down