Repository navigation
Expand file tree
/
Copy pathMySQLHelper.cls
More file actions
169 lines (135 loc) · 5.1 KB
/
Copy pathMySQLHelper.cls
File metadata and controls
169 lines (135 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"Filed out from Dolphin Smalltalk"!
Object subclass: #MySQLHelper
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
classInstanceVariableNames: 'seedOne maxValue seedTwo'!
MySQLHelper guid: (GUID fromString: '{934e2348-35e5-48ce-8126-1cfa126a63dd}')!
MySQLHelper comment: 'MySQLHelper class instanceVariableNames: ''seedOne maxValue seedTwo'''!
!MySQLHelper categoriesForClass!Unclassified! !
!MySQLHelper class methodsFor!
bytesFromHexString: hexString | strm bytes aByte ii high low hexAlphabet | (hexString size \\ 2 = 0) ifFalse: [self error: 'Not proper hex encoding']. strm := ReadStream on: hexString. bytes := ByteArray new: (hexString size // 2). hexAlphabet := '0123456789ABCDEF'. ii := 1. [strm atEnd] whileFalse: [ high := (hexAlphabet indexOf: (strm next asUppercase)) - 1. low := (hexAlphabet indexOf: (strm next asUppercase)) - 1. aByte := high * 16 + low. bytes at: ii put: aByte. ii := ii + 1]. ^ bytes !
bytesFromSha1Hash: num | index bytesCount bytes | index := 0.
bytesCount := num byteSize min: 20. bytes := ByteArray new: (bytesCount max: 20). "gurantee 20 bytes; required for authentication" 20 - bytesCount to: 1 by: -1 do: [ :i | bytes at: (index := index + 1) put: (0 byteAt: 1) ]. "filler 0 from msbyte" bytesCount to: 1 by: -1 do: [ :i | bytes at: (index := index + 1) put: (num byteAt: i) ]. ^ bytes!
decodeLcbFrom: byteStream "parse length coded binary" | first | first := byteStream next. (first between: 0 and: 250) ifTrue: [ ^ first ]. first = 251 ifTrue: [ ^ -1 ]. "column value is NULL; applicable for row data packet only" first = 252 ifTrue: [ ^ byteStream next + (byteStream next bitShift: 8) ]. "value of following 2 bytes" first = 253 ifTrue: [ ^ byteStream next + (byteStream next bitShift: 8) + (byteStream next bitShift: 16) ]. "value of following 3 bytes" first = 254 ifTrue: [ ^ byteStream next + (byteStream next bitShift: 8 * 1) + (byteStream next bitShift: 8 * 2) + (byteStream next bitShift: 8 * 3) + (byteStream next bitShift: 8 * 4) + (byteStream next bitShift: 8 * 5) + (byteStream next bitShift: 8 * 6) + (byteStream next bitShift: 8 * 7) ]. "value of following 8 bytes" ^ nil!
decodeLcsFrom: aStream "parses length coded string" | len | len := self decodeLcbFrom: aStream. len = -1 ifTrue: [^ nil]. ^ aStream next: len. !
encodeLcb: value on: aStream value < -1 ifTrue: [ self error: 'Can not encode value' , value asString , ' to length coded binary' ]. value = -1 ifTrue: [ "represents null column value" ^ aStream nextPut: 251 ]. (value between: 0 and: 250) ifTrue: [ ^ aStream nextPut: (value byteAt: 1) ]. value <= 65535 ifTrue: [ ^ aStream nextPut: 252; nextPut: (value byteAt: 1); nextPut: (value byteAt: 2) ]. value <= 16777215 ifTrue: [ ^ aStream nextPut: 253; nextPut: (value digitAt: 1); nextPut: (value digitAt: 2); nextPut: (value digitAt: 3) yourself ]. ^ aStream nextPut: 254; nextPut: (value digitAt: 1); nextPut: (value digitAt: 2); nextPut: (value digitAt: 3); nextPut: (value digitAt: 4); nextPut: (value digitAt: 5); nextPut: (value digitAt: 6); nextPut: (value digitAt: 7); nextPut: (value digitAt: 8); yourself!
encodeLcs: bytes on: aStream | len | len := bytes size . self encodeLcb: len on: aStream . aStream nextPutAll: bytes . !
hexStringFromBytes: aByteArray ^ aByteArray asHexString asUppercase!
myRandom seedOne := (seedOne * 3 + seedTwo) \\ maxValue. seedTwo := (seedOne + seedTwo + 33) \\ maxValue. ^ (seedOne / maxValue) asFloat !
randomInitWith: seedValue1 and: seedValue2 maxValue := 16r3FFFFFFF. seedOne := seedValue1 \\ maxValue. seedTwo := seedValue2 \\ maxValue.!
secureScramble: seed with: password | sha1 hashStage1 hashStage2 hash3 reply |
(password isNil or: [password isEmpty]) ifTrue: [^#[]].
sha1 := SecureHashAlgorithm new. hashStage1 := (sha1 hashMessage: password) asInteger. hashStage2 := (sha1 hashMessage: (self bytesFromSha1Hash: hashStage1)) asInteger. hash3 := (sha1 hashMessage: seed asByteArray , (self bytesFromSha1Hash: hashStage2)) asInteger. reply := self bytesFromSha1Hash: (hashStage1 bitXor: hash3). ^ reply!
secureScramble256: seed with: password
| lib hashStage1 hashStage2 hashStage3 hash |
(password isNil or: [password isEmpty]) ifTrue: [^#[]].
lib := BCryptLibrary default.
hashStage1 := lib sha256Hash: password asByteArray.
hashStage2 := lib sha256Hash: hashStage1.
hashStage3 := lib sha256Hash: (hashStage2, seed).
hash := ByteArray new: hashStage1 size.
1 to: hash size do: [ :index | hash at: index put: ((hashStage1 at: index) bitXor: (hashStage3 at: index))].
^hash! !
!MySQLHelper class categoriesForMethods!
bytesFromHexString:!public!utilities! !
bytesFromSha1Hash:!public!utilities! !
decodeLcbFrom:!encoding!public! !
decodeLcsFrom:!encoding!public! !
encodeLcb:on:!encoding!public! !
encodeLcs:on:!encoding!public! !
hexStringFromBytes:!public!utilities! !
myRandom!private! !
randomInitWith:and:!private! !
secureScramble:with:!public!scrambling! !
secureScramble256:with:!public!scrambling! !
!