Exemple #1
0
        // PKCS #1 v.2.1, Section 9.2
        // EMSA-PKCS1-v1_5-Encode
        static byte[] Encode_v15(HashAlgorithmType algorithm, byte[] hashValue, int emLength)
        {
            var hashSize = HashAlgorithmProvider.GetHashSize(algorithm);

            if (hashValue.Length != (hashSize >> 3))
            {
                throw new CryptographicException("bad hash length for " + algorithm.ToString());
            }

            // DigestInfo ::= SEQUENCE {
            //	digestAlgorithm AlgorithmIdentifier,
            //	digest OCTET STRING
            // }

            byte[] t = null;

            string oid = HashAlgorithmProvider.GetOID(algorithm);

            if (oid != null)
            {
                ASN1 digestAlgorithm = new ASN1(0x30);
                digestAlgorithm.Add(new ASN1(CryptoConfig.EncodeOID(oid)));
                digestAlgorithm.Add(new ASN1(0x05));                            // NULL
                ASN1 digest     = new ASN1(0x04, hashValue);
                ASN1 digestInfo = new ASN1(0x30);
                digestInfo.Add(digestAlgorithm);
                digestInfo.Add(digest);

                t = digestInfo.GetBytes();
            }
            else
            {
                // There are no valid OID, in this case t = hashValue
                // This is the case of the MD5SHA hash algorithm
                t = hashValue;
            }

            Buffer.BlockCopy(hashValue, 0, t, t.Length - hashValue.Length, hashValue.Length);

            int PSLength = System.Math.Max(8, emLength - t.Length - 3);

            // PS = PSLength of 0xff

            // EM = 0x00 | 0x01 | PS | 0x00 | T
            byte[] EM = new byte [PSLength + t.Length + 3];
            EM [1] = 0x01;
            for (int i = 2; i < PSLength + 2; i++)
            {
                EM[i] = 0xff;
            }
            Buffer.BlockCopy(t, 0, EM, PSLength + 3, t.Length);

            return(EM);
        }
 public override void SetHashAlgorithm(string strName)
 {
     hash = HashAlgorithmProvider.CreateAlgorithmFromName(strName);
 }