Coverage for mindsdb / api / mysql / mysql_proxy / external_libs / mysql_scramble.py: 28%

80 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-21 00:36 +0000

1""" 

2MIT License 

3============ 

4Copyright (c) 2010, 2013 PyMySQL contributors 

5 

6Permission is hereby granted, free of charge, to any person obtaining a copy 

7of this software and associated documentation files (the "Software"), to deal 

8in the Software without restriction, including without limitation the rights 

9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 

10copies of the Software, and to permit persons to whom the Software is 

11furnished to do so, subject to the following conditions: 

12 

13The above copyright notice and this permission notice shall be included in 

14all copies or substantial portions of the Software. 

15 

16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 

22THE SOFTWARE. 

23""" 

24 

25import hashlib 

26from functools import partial 

27import struct 

28import io 

29import sys 

30 

31PYPY = hasattr(sys, 'pypy_translation_info') 

32JYTHON = sys.platform.startswith('java') 

33IRONPYTHON = sys.platform == 'cli' 

34CPYTHON = not PYPY and not JYTHON and not IRONPYTHON 

35 

36range_type = range 

37text_type = str 

38long_type = int 

39str_type = str 

40unichr = chr 

41 

42sha_new = partial(hashlib.new, 'sha1') 

43 

44 

45def scramble(password, message): 

46 SCRAMBLE_LENGTH = 20 

47 stage1 = sha_new(password.encode('utf-8')).digest() 

48 stage2 = sha_new(stage1).digest() 

49 s = sha_new() 

50 s.update(message[:SCRAMBLE_LENGTH].encode('utf-8')) 

51 s.update(stage2) 

52 result = s.digest() 

53 return _my_crypt(result, stage1) 

54 

55 

56def _my_crypt(message1, message2): 

57 length = len(message1) 

58 result = b'' 

59 for i in range_type(length): 

60 x = (struct.unpack('B', message1[i:i + 1])[0] 

61 ^ struct.unpack('B', message2[i:i + 1])[0]) 

62 result += struct.pack('B', x) 

63 return result 

64 

65 

66# old_passwords support ported from libmysql/password.c 

67SCRAMBLE_LENGTH_323 = 8 

68 

69 

70class RandStruct_323(object): 

71 def __init__(self, seed1, seed2): 

72 self.max_value = 0x3FFFFFFF 

73 self.seed1 = seed1 % self.max_value 

74 self.seed2 = seed2 % self.max_value 

75 

76 def my_rnd(self): 

77 self.seed1 = (self.seed1 * 3 + self.seed2) % self.max_value 

78 self.seed2 = (self.seed1 + self.seed2 + 33) % self.max_value 

79 return float(self.seed1) / float(self.max_value) 

80 

81 

82def scramble_323(password, message): 

83 hash_pass = _hash_password_323(password) 

84 hash_message = _hash_password_323(message[:SCRAMBLE_LENGTH_323]) 

85 hash_pass_n = struct.unpack(">LL", hash_pass) 

86 hash_message_n = struct.unpack(">LL", hash_message) 

87 

88 rand_st = RandStruct_323(hash_pass_n[0] ^ hash_message_n[0], 

89 hash_pass_n[1] ^ hash_message_n[1]) 

90 outbuf = io.BytesIO() 

91 for _ in range_type(min(SCRAMBLE_LENGTH_323, len(message))): 

92 outbuf.write(int2byte(int(rand_st.my_rnd() * 31) + 64)) 

93 extra = int2byte(int(rand_st.my_rnd() * 31)) 

94 out = outbuf.getvalue() 

95 outbuf = io.BytesIO() 

96 for c in out: 

97 outbuf.write(int2byte(byte2int(c) ^ byte2int(extra))) 

98 return outbuf.getvalue() 

99 

100 

101def _hash_password_323(password): 

102 nr = 1345345333 

103 add = 7 

104 nr2 = 0x12345671 

105 

106 # x in py3 is numbers, p27 is chars 

107 for c in [byte2int(x) for x in password if x not in (' ', '\t', 32, 9)]: 

108 nr ^= (((nr & 63) + add) * c) + (nr << 8) & 0xFFFFFFFF 

109 nr2 = (nr2 + ((nr2 << 8) ^ nr)) & 0xFFFFFFFF 

110 add = (add + c) & 0xFFFFFFFF 

111 

112 r1 = nr & ((1 << 31) - 1) # kill sign bits 

113 r2 = nr2 & ((1 << 31) - 1) 

114 return struct.pack(">LL", r1, r2) 

115 

116 

117def byte2int(b): 

118 if isinstance(b, int): 

119 return b 

120 else: 

121 return struct.unpack("!B", b)[0] 

122 

123 

124def int2byte(i): 

125 return struct.pack("!B", i) 

126 

127 

128def join_bytes(bs): 

129 if len(bs) == 0: 

130 return "" 

131 else: 

132 rv = bs[0] 

133 for b in bs[1:]: 

134 rv += b 

135 return rv