Esempio n. 1
0
        private byte[] ComputeHash(EstEIDReader estEidReader, byte[] data)
        {
            uint       rc, digest_length = 0;
            const uint hash_length = Digest.SHA1_LENGTH;

            Digest hash = new Digest(estEidReader);

            rc = hash.InitDigest(Digest.HashAlgorithm.SHA1);
            if (rc != EstEIDReader.ESTEID_OK)
            {
                throw new Exception(Resources.CARD_HASH_INIT);
            }

            rc = hash.UpdateDigest(data, (uint)data.Length);
            if (rc != EstEIDReader.ESTEID_OK)
            {
                throw new Exception(Resources.CARD_HASH_UPDATE);
            }

            HGlobalSafeHandle raw       = new HGlobalSafeHandle((int)hash_length);
            IntPtr            hashBytes = raw;

            if (hashBytes == IntPtr.Zero)
            {
                throw new OutOfMemoryException(Resources.OUT_OF_MEMORY);
            }

            rc = hash.FinalizeDigest(ref hashBytes, ref digest_length);
            if (rc != EstEIDReader.ESTEID_OK)
            {
                throw new Exception(Resources.CARD_HASH_FINALIZE);
            }

            return(raw.ToByteArray());
        }
Esempio n. 2
0
        private byte[] ComputeHash(EstEIDReader estEidReader, PdfSignatureAppearance sap)
        {
            Digest     hash          = new Digest(estEidReader);
            const uint hash_length   = Digest.SHA1_LENGTH;
            uint       digest_length = 0;
            uint       rc;

            rc = hash.InitDigest(Digest.HashAlgorithm.SHA1);
            if (rc != EstEIDReader.ESTEID_OK)
            {
                throw new Exception(Resources.CARD_HASH_INIT);
            }

            Stream       s    = sap.RangeStream;
            MemoryStream ss   = new MemoryStream();
            int          read = 0;

            byte[] buff = new byte[8192];
            while ((read = s.Read(buff, 0, 8192)) > 0)
            {
                ss.Write(buff, 0, read);
                rc = hash.UpdateDigest(buff, (uint)read);
                if (rc != EstEIDReader.ESTEID_OK)
                {
                    throw new Exception(Resources.CARD_HASH_UPDATE);
                }
            }

            HGlobalSafeHandle raw       = new HGlobalSafeHandle((int)hash_length);
            IntPtr            hashBytes = raw;

            if (hashBytes == IntPtr.Zero)
            {
                throw new OutOfMemoryException(Resources.OUT_OF_MEMORY);
            }

            rc = hash.FinalizeDigest(ref hashBytes, ref digest_length);
            if (rc != EstEIDReader.ESTEID_OK)
            {
                throw new Exception(Resources.CARD_HASH_FINALIZE);
            }

            return(raw.ToByteArray());
        }