Written by

Senior Developer at Aquaservice
Question Daniel Aguilar · 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

Comments

Daniel Aguilar · Nov 29, 2023

I answer my self xD

##class(%SYSTEM.Encryption).MD5HashStream(stream)

I hope it can be useful for someone too xD

Thanks!

0
Yaron Munz · Nov 29, 2023

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)

0
Daniel Aguilar  Nov 29, 2023 to Yaron Munz

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)

0
Julius Kavay · Nov 29, 2023

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?

0