· Nov 29, 2023

$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!

Product version: IRIS 2023.2
Discussion (4)1
Log in or sign up to continue

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=0
        while 'str.AtEnd { s crc=$zcrc(str.Read(32000),typ,crc) }
        ret crc
    } else { ret $zcrc(str,typ) }

Where is the problem?