$ZCRC with Streams
Is there any way to perfom a cyclick redundancy of a "stream" instead of string?
I know exists the method $ZCRC but I think it's only for strings.
Thank you!
Comments
I answer my self xD
##class(%SYSTEM.Encryption).MD5HashStream(stream)
I hope it can be useful for someone too xD
Thanks!
As you probably know, a stream is a "collection" of strings ("under the hood"). As the $ZCRC doesn't support incremental hashing you need to choose some other hashing that support this: SHA-256, MD5 (not recommended due to security vulnerabilities)
Thanks Yaron,
I'm not worried about security because I plan to use it on non-critical internal processes, but anyway I've done some research and here is an example using SHA-256:
Set encryptedValue = ##class(%SYSTEM.Encryption).SHAHashStream(256,stream)
The first parameter (in my case the value 256) refers to the bit-length:
160 (SHA-1)
224 (SHA-224)
256 (SHA-256)
384 (SHA-384)
512 (SHA-512)
If you want, for whatever reason, to compute the CRC of a Stream then write an short method like this (or use this):
/// Input : str - a sting or a stream/// typ - CRC-Type (0..7, See ISC documentation)/// /// Output: crc-value/// ClassMethod CRC(str, typ = 7)
{
i $isobject(str),str.%IsA("%Stream.Object") {
d str.Rewind() s crc=0while 'str.AtEnd { s crc=$zcrc(str.Read(32000),typ,crc) }
ret crc
} else { ret $zcrc(str,typ) }
}Where is the problem?