From VisualWorks®, 7.6 of February 1, 2008 on February 1, 2008 at 12:31:22 pm (Dialog confirm: 'You are filing-in a Parcel source file!\\While this is possible it will not have\the same effect as loading the parcel.\None of the Parcel''s prerequisites will\be loaded and none of its load actions\will be performed.\\Are you sure you want to file-in?' withCRs) ifFalse: [self error: 'Parcel file-in abandoned. Choose terminate or close.'] <name>Security</name> <environment>Smalltalk</environment> <private>false</private> <imports> private Smalltalk.* </imports> <category>SecurityBase</category> <attributes> <package>SecurityBase</package> </attributes> <class> <name>ThirtyTwoBitRegister</name> <environment>Security</environment> <super>Core.Object</super> <private>false</private> none hi low <imports></imports> <category>Security-Hashes</category> <attributes> <package>SecurityBase</package> </attributes> </class> <comment> Security.ThirtyTwoBitRegister
Increases efficiency of computations on 32-bit numbers by avoiding overflow into the large intergers. Works by splitting the 32-bit value in half to which are guaranteed to fit into the SmallInteger range. Instance Variables: hi <SmallInteger> the more significant half of the register low <SmallInteger> the less significant half of the register
</comment> <methods> Security.ThirtyTwoBitRegister <category>accessing</category>
byte1 ^hi bitShift: -8
byte2 ^hi bitAnd: 255
byte3 ^low bitShift: -8
byte4 ^low bitAnd: 255
hi ^ hi
hi: anInteger hi := anInteger
hi: anInteger1 low: anInteger2 hi := anInteger1. low := anInteger2
low ^ low
low: anInteger low := anInteger
</methods> <methods> Security.ThirtyTwoBitRegister <category>operations</category>
+= aThirtyTwoBitRegister "Replace my contents with the sum of the given register and my current contents." low := low + aThirtyTwoBitRegister low. hi := (hi + aThirtyTwoBitRegister hi + (low > 16rFFFF ifTrue: [1] ifFalse: [0] ) ) bitAnd: 16rFFFF. low := low bitAnd: 16rFFFF
<< shift | tl | shift > 16 ifTrue: [hi := (low bitShift: shift - 16) bitAnd: 65535. low := 0] ifFalse: [tl := low bitShift: shift - 16. low := (low bitShift: shift) bitAnd: 65535. hi := ((hi bitShift: shift) bitAnd: 65535) + tl]
= aThirtyTwoBitRegister "fixed for AR 47784 because the runtime package breaks with this etc. fastEqual: is implemeted to do the comparision with out the extra cycle of having to check the class name." ^ self class == aThirtyTwoBitRegister class and: [hi = aThirtyTwoBitRegister hi and: [low = aThirtyTwoBitRegister low]].
>> shift low := low bitAnd: 16rffff. low := ((hi bitShift: 16 - shift ) bitAnd: 16rFFFF) bitOr: (low bitShift: shift negated). hi :=(hi bitShift: shift negated).
addIn: a32BitRegister "There's +=, but I want consistent naming and there's no good binary equivalent for xor" low := low + a32BitRegister low. hi := (hi + a32BitRegister hi + (low > 16rFFFF ifTrue: [1] ifFalse: [0]) ) bitAnd: 16rFFFF. low := low bitAnd: 16rFFFF
bitAnd: aThirtyTwoBitRegister hi := hi bitAnd: aThirtyTwoBitRegister hi. low := low bitAnd: aThirtyTwoBitRegister low.
bitInvert hi := hi bitXor: 16rFFFF. low := low bitXor: 16rFFFF.
bitOr: aThirtyTwoBitRegister hi := hi bitOr: aThirtyTwoBitRegister hi. low := low bitOr: aThirtyTwoBitRegister low.
bitXor: aThirtyTwoBitRegister hi := hi bitXor: aThirtyTwoBitRegister hi. low := low bitXor: aThirtyTwoBitRegister low.
fastEqual: aThirtyTwoBitRegister "Put here as a result of AR 47784 to have an = that doesn't have to do a class name comparison everytime " ^(hi = aThirtyTwoBitRegister hi) and:[ low = aThirtyTwoBitRegister low]
leftRotateBy: bits | count | count := bits \\ 32. count > 16 ifTrue: [ | left right newHi | left := count - 16. right := left - 16. newHi := (low specialLeftShift: left) bitOr: (hi bitShift: right). low := (hi specialLeftShift: left) bitOr: (low bitShift: right). hi := newHi] ifFalse: [ | left right newHi | left := count. right := left - 16. newHi := (hi specialLeftShift: left) bitOr: (low bitShift: right). low := (low specialLeftShift: left) bitOr: (hi bitShift: right). hi := newHi]
rightRotateBy: bits ^self leftRotateBy: 32 - bits
set: a32BitRegister hi := a32BitRegister hi. low := a32BitRegister low
xorIn: a32BitRegister "Again added just for more consistent naming" low := low bitXor: a32BitRegister low. hi := hi bitXor: a32BitRegister hi
</methods> <methods> Security.ThirtyTwoBitRegister <category>conversions</category>
asInteger "Answer the integer value" | int | int := LargePositiveInteger basicNew: 4. int basicAt: 3 put: (hi bitAnd: 16rff). int basicAt:4 put: ((hi bitShift: 0 - 8) bitAnd: 16rff). int basicAt: 1 put: (low bitAnd: 16rff). int basicAt:2 put: ((low bitShift: 0 - 8) bitAnd: 16rff). ^int "^ (hi bitShift: 16) + low"
load: anInteger "Set my contents to the value of given integer." low := anInteger bitAnd: 16rFFFF. hi := (anInteger bitShift: -16) bitAnd: 16rFFFF. " self asInteger = anInteger ifFalse: [self error: 'out of range: ', anInteger printString]."
loadFrom: aByteArray at: index "Load my 32-bit value from the four bytes of the given ByteArray starting at the given index. Consider the first byte to contain the most significant bits of the word (i.e., use big-endian byte ordering)." hi := ((aByteArray at: index) bitShift: 8) + ( aByteArray at: index + 1). low := ((aByteArray at: index + 2) bitShift: 8) + ( aByteArray at: index + 3).
reverseLoadFrom: aByteArray at: index "Load my 32-bit value from the four bytes of the given ByteArray starting at the given index. Consider the first byte to contain the most significant bits of the word (i.e., use little-endian byte ordering)." hi := ((aByteArray at: index+ 3) bitShift: 8) + ( aByteArray at: index + 2). low := ((aByteArray at: index + 1) bitShift: 8) + ( aByteArray at: index ).
reverseStoreTo: aByteArray at: index "Little-endian" aByteArray at: index put: (low bitAnd: 255); at: index + 1 put: (low bitShift: -8); at: index + 2 put: (hi bitAnd: 255); at: index + 3 put: (hi bitShift: -8)
storeTo: aByteArray at: index "Big-endian" aByteArray at: index put: (hi bitShift: -8); at: index + 1 put: (hi bitAnd: 255); at: index + 2 put: (low bitShift: -8); at: index + 3 put: (low bitAnd: 255)
</methods> <methods> Security.ThirtyTwoBitRegister <category>printing</category>
printOn: aStream "Print my contents in hex put an R in front to show that it is a register being printed." aStream nextPutAll: (#RegisterPrefix << #security >> 'R:') asString. self asInteger printOn: aStream base: 16. aStream nextPutAll: (#DecimalPrefix << #security >> ' D:') asString. self asInteger printOn: aStream base: 10.
</methods> <methods> Security.ThirtyTwoBitRegister class <category>instance creation</category>
new "Answer a new instance whose initial contents is zero." ^ super new load: 0
</methods> <methods> Core.LargeNegativeInteger <category>security</category>
asBigEndianByteArray "Convert receiver to a byte array of apropriate size in network (a.k.a big endian) format. Not supported for negative integers." ^self errorConvertingNegativeIntegerToBytes
asBigEndianByteArrayPaddedTo: numberOfBytes "Encode the receiver in the network (aka big endian) order. Pad the byte array in the beginning to size anInteger. anInteger bytes has to be enough to store the receiver. The receiver is expected to be non-negative." ^self errorConvertingNegativeIntegerToBytes
</methods> <methods> Core.SmallInteger class <category>hashes</category>
leftShiftArray ^#( 2r0111111111111111 2r0011111111111111 2r0001111111111111 2r0000111111111111 2r0000011111111111 2r0000001111111111 2r0000000111111111 2r0000000011111111 2r0000000001111111 2r0000000000111111 2r0000000000011111 2r0000000000001111 2r0000000000000111 2r0000000000000011 2r0000000000000001 2r0000000000000000 )
</methods> <methods> Core.SmallInteger <category>security</category>
asBigEndianByteArray "Convert receiver to a byte array of apropriate size in network (a.k.a big endian) format. Not supported for negative integers." self negative ifTrue: [ self errorConvertingNegativeIntegerToBytes ]. ^self > 65535 ifTrue: [ | high low | high := self bitShift: -16. low := self bitAnd: 65535. high > 255 ifTrue: [ ByteArray with: (high bitShift: -8) with: (high bitAnd: 255) with: (low bitShift: -8) with: (low bitAnd: 255) ] ifFalse: [ ByteArray with: (high bitAnd: 255) with: (low bitShift: -8) with: (low bitAnd: 255) ] ] ifFalse: [ self > 255 ifTrue: [ ByteArray with: (self bitShift: -8) with: (self bitAnd: 255) ] ifFalse: [ ByteArray with: (self bitAnd: 255) ] ]
specialLeftShift: aShift " yes this is the just like the bitShift: message but I am saving some cyles since I know the number that I am shifting over. so if it can't be done with a small integer result d the shift left with the values that I know have to be in there. this saves a few sends and test which done over a million iterations saves time especially with the block ciphers since we handle only 8 bytes at a time so to do a megabyte requires 128000 iterations the faster each of those iterations the more I can encrypt in each second so every cycle counts so since I am only in this case concerned with 16 bits only mask before the shift to guarantee that I will always result with a small int in the primative. aShift must be betwwen 0 and 16" aShift = 0 ifFalse:[ ^(self bitAnd: (self class leftShiftArray at: aShift)) bitShift: aShift]
</methods> <methods> Core.ByteArray class <category>encryption</category>
fromHexString: aString "(((self fromHexString: 'A00B0210FF00')))" | ba s lookup | lookup := '123456789ABCDEF'. ba := self new: aString size // 2. s := aString readStream. 1 to: ba size do: [ :i || h l | h := s next. h := h = $0 ifTrue: [0] ifFalse: [lookup indexOf: h]. l := s next. l := l = $0 ifTrue: [0] ifFalse: [lookup indexOf: l]. ba at: i put: ((h bitShift: 4) bitOr: l) ]. ^ba
</methods> <methods> Core.ByteArray <category>security</category>
asHexString "(((#[160 11 2 16 255 0] asHexString)))" | lookup s ws | lookup := '0123456789ABCDEF'. s := String new: self size * 2. ws := s writeStream. self do: [ :b | ws nextPut: (lookup at: (b bitShift: -4) + 1). ws nextPut: (lookup at: (b bitAnd: 15) + 1) ]. ^s
asLargePositiveInteger "do the reverse of the asByteArray in LargePositiveInteger" | int | int := LargePositiveInteger basicNew: self size. self keysAndValuesDo: [ :index :each | int basicAt: (self size + 1- index) put: each ]. ^int
</methods> <methods> Core.LargePositiveInteger <category>security</category>
asBigEndianByteArray "Convert receiver to a byte array of apropriate size in network (a.k.a big endian) format. Not supported for negative integers." | answer top | top := self basicSize + 1. answer := ByteArray new: top - 1. 1 to: top - 1 do: [:i | answer at: i put: (self basicAt: top - i)]. ^answer
asBigEndianByteArrayPaddedTo: numberOfBytes "Encode the receiver in the network (aka big endian) order. Pad the byte array in the beginning to size anInteger. anInteger bytes has to be enough to store the receiver. The receiver is expected to be non-negative." | answer top pad | self basicSize > numberOfBytes ifTrue: [ self errorInsufficientSpaceForIntegerToBytesConversion ]. top := self basicSize + 1. pad := numberOfBytes - top + 1. answer := ByteArray new: numberOfBytes. 1 to: top - 1 do: [:i | answer at: pad + i put: (self basicAt: top - i)]. ^answer
</methods> <methods> Core.Integer <category>security</category>
asBigEndianByteArrayPaddedTo: anInteger "Encode the receiver in the network (aka big endian) order. Pad the byte array in the beginning to size anInteger. anInteger bytes has to be enough to store the receiver. The receiver is expected to be non-negative." | ba counter number | self negative ifTrue: [ self errorConvertingNegativeIntegerToBytes ]. ba := ByteArray new: anInteger. counter := anInteger. number := self. [number > 0] whileTrue: [ counter = 0 ifTrue: [ self errorInsufficientSpaceForIntegerToBytesConversion ]. ba at: counter put: (number bitAnd: 255). counter := counter - 1. number := number bitShift: -8 ]. ^ba
</methods> <methods> Core.Integer <category>deprecated</category>
asByteArray "WARNING: This selector is too ambiguous and is being deprecated. It will be removed in the next release." ^self asBigEndianByteArray
asByteArrayPaddedTo: anInteger "WARNING: This selector is too ambiguous and is being deprecated. It will be removed in the next release." ^self asBigEndianByteArrayPaddedTo: anInteger
</methods> <methods> Core.Integer <category>security</category>
errorConvertingNegativeIntegerToBytes self error: (#errCantConvertNegativeNumbers << #security >> 'Cannot convert negative integer into a big-endian byte array').
errorInsufficientSpaceForIntegerToBytesConversion self error: (#InsufficientSize << #security >> 'Not enough room for the whole number').
</methods> "Imported Classes:" self error: 'Attempting to file-in parcel imports. Choose terminate or close' <class> <name>Object</name> <environment>Core</environment> <super></super> <private>false</private> none <imports></imports> <category>Kernel-Objects</category> <attributes> <package>Kernel-Objects</package> </attributes> </class> <class> <name>Integer</name> <environment>Core</environment> <super>Core.Number</super> <private>false</private> none <imports></imports> <category>Magnitude-Numbers</category> <attributes> <package>Magnitude-Numbers</package> </attributes> </class> <class> <name>SmallInteger</name> <environment>Core</environment> <super>Core.Integer</super> <private>false</private> immediate minVal maxVal maxBits maxBytes <imports></imports> <category>Magnitude-Numbers</category> <attributes> <package>Magnitude-Numbers</package> </attributes> </class> <class> <name>LargePositiveInteger</name> <environment>Core</environment> <super>Core.LargeInteger</super> <private>false</private> bytes <imports></imports> <category>Magnitude-Numbers</category> <attributes> <package>Magnitude-Numbers</package> </attributes> </class> <class> <name>ByteArray</name> <environment>Core</environment> <super>Core.IntegerArray</super> <private>false</private> bytes <imports></imports> <category>Collections-Arrayed</category> <attributes> <package>Collections-Arrayed</package> </attributes> </class> <class> <name>LargeNegativeInteger</name> <environment>Core</environment> <super>Core.LargeInteger</super> <private>false</private> bytes <imports></imports> <category>Magnitude-Numbers</category> <attributes> <package>Magnitude-Numbers</package> </attributes> </class>