Goedel.Cryptography.UDF.From C# (CSharp) Method

From() public static method

Compute UDF from binary data and content type with specified precision.
public static From ( string ContentType, byte Data, int Bits ) : byte[]
ContentType string MIME media type. See /// http://www.iana.org/assignments/media-types/media-types.xhtml for list.
Data byte Data to be fingerprinted.
Bits int Precision, must be a multiple of 25 bits.
return byte[]
        public static byte[] From(string ContentType, byte[] Data, int Bits) {
            SHA512 SHA512 = new SHA512Cng();
            byte[] HashData = SHA512.ComputeHash(Data);

            SHA512.Initialize();

            var Tag = Encoding.UTF8.GetBytes(ContentType);
            byte[] Input = new byte[HashData.Length + Tag.Length + 1];

            int i = 0;
            foreach (var Byte in Tag) {
                Input[i++] = Byte;
                }
            Input[i++] = (byte)':';
            foreach (var Byte in HashData) {
                Input[i++] = Byte;
                }

            SHA512.Initialize();
            byte[] UDFData = SHA512.ComputeHash(Input);

            var TotalBits = Bits;
            var FullBytes = TotalBits / 8;
            var ExtraBits = TotalBits % 8;
            var TotalBytes = ExtraBits == 0 ? FullBytes : FullBytes + 1;

            byte[] Output = new byte[TotalBytes];
            Output[0] = (byte)UDFConstants.KeyIdentifierAlgSHA_2_512;
            for (var j = 0; j < FullBytes - 1; j++) {
                Output[j + 1] = UDFData[j];
                }

            if (ExtraBits > 0) {
                Output[TotalBytes - 1] = (byte)(UDFData[FullBytes - 1] << (8 - ExtraBits) & 0xff);
                }

            return Output;
            }