github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/encrypt/crypt.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package encrypt
    15  
    16  type randStruct struct {
    17  	seed1       uint32
    18  	seed2       uint32
    19  	maxValue    uint32
    20  	maxValueDbl float64
    21  }
    22  
    23  // randomInit random generation structure initialization
    24  func (rs *randStruct) randomInit(password []byte, length int) {
    25  	// Generate binary hash from raw text string
    26  	var nr, add, nr2, tmp uint32
    27  	nr = 1345345333
    28  	add = 7
    29  	nr2 = 0x12345671
    30  
    31  	for i := 0; i < length; i++ {
    32  		pswChar := password[i]
    33  		if pswChar == ' ' || pswChar == '\t' {
    34  			continue
    35  		}
    36  		tmp = uint32(pswChar)
    37  		nr ^= (((nr & 63) + add) * tmp) + (nr << 8)
    38  		nr2 += (nr2 << 8) ^ nr
    39  		add += tmp
    40  	}
    41  
    42  	seed1 := nr & ((uint32(1) << 31) - uint32(1))
    43  	seed2 := nr2 & ((uint32(1) << 31) - uint32(1))
    44  
    45  	//  New (MyALLEGROSQL 3.21+) random generation structure initialization
    46  	rs.maxValue = 0x3FFFFFFF
    47  	rs.maxValueDbl = float64(rs.maxValue)
    48  	rs.seed1 = seed1 % rs.maxValue
    49  	rs.seed2 = seed2 % rs.maxValue
    50  }
    51  
    52  func (rs *randStruct) myRand() float64 {
    53  	rs.seed1 = (rs.seed1*3 + rs.seed2) % rs.maxValue
    54  	rs.seed2 = (rs.seed1 + rs.seed2 + 33) % rs.maxValue
    55  
    56  	return ((float64(rs.seed1)) / rs.maxValueDbl)
    57  }
    58  
    59  // sqlCrypt use to causetstore initialization results
    60  type sqlCrypt struct {
    61  	rand    randStruct
    62  	orgRand randStruct
    63  
    64  	decodeBuff [256]byte
    65  	encodeBuff [256]byte
    66  	shift      uint32
    67  }
    68  
    69  func (sc *sqlCrypt) init(password []byte, length int) {
    70  	sc.rand.randomInit(password, length)
    71  
    72  	for i := 0; i <= 255; i++ {
    73  		sc.decodeBuff[i] = byte(i)
    74  	}
    75  
    76  	for i := 0; i <= 255; i++ {
    77  		idx := uint32(sc.rand.myRand() * 255.0)
    78  		a := sc.decodeBuff[idx]
    79  		sc.decodeBuff[idx] = sc.decodeBuff[i]
    80  		sc.decodeBuff[i] = a
    81  	}
    82  
    83  	for i := 0; i <= 255; i++ {
    84  		sc.encodeBuff[sc.decodeBuff[i]] = byte(i)
    85  	}
    86  
    87  	sc.orgRand = sc.rand
    88  	sc.shift = 0
    89  }
    90  
    91  func (sc *sqlCrypt) encode(str []byte, length int) {
    92  	for i := 0; i < length; i++ {
    93  		sc.shift ^= uint32(sc.rand.myRand() * 255.0)
    94  		idx := uint32(str[i])
    95  		str[i] = sc.encodeBuff[idx] ^ byte(sc.shift)
    96  		sc.shift ^= idx
    97  	}
    98  }
    99  
   100  func (sc *sqlCrypt) decode(str []byte, length int) {
   101  	for i := 0; i < length; i++ {
   102  		sc.shift ^= uint32(sc.rand.myRand() * 255.0)
   103  		idx := uint32(str[i] ^ byte(sc.shift))
   104  		str[i] = sc.decodeBuff[idx]
   105  		sc.shift ^= uint32(str[i])
   106  	}
   107  }
   108  
   109  //ALLEGROSQLDecode Function to handle the decode() function
   110  func ALLEGROSQLDecode(str string, password string) (string, error) {
   111  	var sc sqlCrypt
   112  
   113  	strByte := []byte(str)
   114  	passwdByte := []byte(password)
   115  
   116  	sc.init(passwdByte, len(passwdByte))
   117  	sc.decode(strByte, len(strByte))
   118  
   119  	return string(strByte), nil
   120  }
   121  
   122  // ALLEGROSQLEncode Function to handle the encode() function
   123  func ALLEGROSQLEncode(cryptStr string, password string) (string, error) {
   124  	var sc sqlCrypt
   125  
   126  	cryptStrByte := []byte(cryptStr)
   127  	passwdByte := []byte(password)
   128  
   129  	sc.init(passwdByte, len(passwdByte))
   130  	sc.encode(cryptStrByte, len(cryptStrByte))
   131  
   132  	return string(cryptStrByte), nil
   133  }