Cryptographic API Functions/en
All algorithms are accessed the same way, by either sending one of the convenient utility messages to a hash-stream-class, or by instantiating a hash stream and sending it the data to be hashed.
The utility messages are easier to use, but require the data to be hashed to be already present. The instance messages allow for streams of unknown length to be hashed, to preinitialize the parameters and to fetch/reset the hash value during the operation.
Available hash classes are: MD5Stream
, SHA1Stream
, SHA256Stream
etc.
(open a browser or evaluate "HashStream allSubclasses" to see them all).
Utility (Class-Side) Protocol[Bearbeiten]
HashStreamClass hashValueOf:
aStringOrByteArray
- Returns the hash value as a byte vector (the length of the vector depends on the algorithm).
- Example:
MD5Stream hashValueOf:
'hello world' => #[94 182 59 187 224 30 238 208 147 203 34 187 143 90 205 195]SHA1Stream hashValueOf:
'hello world' => #[42 174 108 53 201 79 207 180 21 219 233 95 64 139 156 233 30 232 70 237]SHA256Stream hashValueOf:
'hello world' => #[185 77 39 185 147 77 62 8 165 46 82 215 218 125 171 250 196 132 239 227 122 83 128 238 144 136 247 172 226 239 205 233]
HashStreamClass hashValueHexString:
aStringOrByteArray
- Returns the hash value as a hex string.
- Example:
MD5Stream hashValueHexString:
'hello world' => '5eb63bbbe01eeed093cb22bb8f5acdc3'SHA1Stream hashValueHexString:
'hello world' => '2aae6c35c94fcfb415dbe95f408b9ce91ee846ed'SHA256Stream hashValueHexString:
'hello world' => 'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'
HashStreamClass hashValueOfFile:
aFilename
- Returns the hash of a file's contents.
- Example:
MD5Stream hashValueOfFile:
'Makefile'SHA256Stream hashValueOfFile:
'Makefile'
Instance Protocol[Bearbeiten]
More fine control is available by instantiating a hash-stream and sending it the data. First, create an instance:
myHashStream := HashStreamClass new
.
then send it the bytes to be hashed with the usual stream messages:
myHashStream nextPutAll: stringOrByteCollection myHashStream nextPut: characterOrSingleByteValue
and finally ask the stream for its current (running) hashValue with:
myHashStream hashValue
Thus, the above utility in the MD5Stream is equivalent to:
myStream := MD5Stream new. myStream nextPutAll:'hello world'. hashBytes := myStream hashValue. hashHexString := myStream hashValue hexPrintString asLowercase.