Inheritance: LongDigest
Ejemplo n.º 1
0
        public override void Reset(IMemoable other)
        {
            Sha384Digest t = (Sha384Digest)other;

            CopyIn(t);
        }
Ejemplo n.º 2
0
        public static byte[] Digest(byte[] data, String algo)
        {
            if (algo == null)
            {
                throw new ArgumentNullException("El algoritmo de huella digital no puede ser nulo");
            }
            if (data == null)
            {
                throw new ArgumentNullException("Los datos no pueden ser nulos");
            }

            switch (algo)
            {
                /**
                 * ALGORITMOS DE HASING
                 */
                case AOSignConstants.SIGN_ALGORITHM_SHA1:
                    {
                        Sha1Digest dig = new Sha1Digest();
                        dig.BlockUpdate(data, 0, data.Length);
                        byte[] result = new byte[dig.GetDigestSize()];
                        dig.DoFinal(result, 0);
                        return result;
                    }

                case AOSignConstants.SIGN_ALGORITHM_SHA256:
                    {
                        Sha256Digest dig = new Sha256Digest();
                        dig.BlockUpdate(data, 0, data.Length);
                        byte[] result = new byte[dig.GetDigestSize()];
                        dig.DoFinal(result, 0);
                        return result;
                    }

                case AOSignConstants.SIGN_ALGORITHM_SHA384:
                    {
                        Sha384Digest dig = new Sha384Digest();
                        dig.BlockUpdate(data, 0, data.Length);
                        byte[] result = new byte[dig.GetDigestSize()];
                        dig.DoFinal(result, 0);
                        return result;
                    }

                case AOSignConstants.SIGN_ALGORITHM_SHA512:
                    {
                        Sha512Digest dig = new Sha512Digest();
                        dig.BlockUpdate(data, 0, data.Length);
                        byte[] result = new byte[dig.GetDigestSize()];
                        dig.DoFinal(result, 0);
                        return result;
                    }

                case AOSignConstants.SIGN_ALGORITHM_RIPEMD160:
                    {
                        RipeMD160Digest dig = new RipeMD160Digest();
                        dig.BlockUpdate(data, 0, data.Length);
                        byte[] result = new byte[dig.GetDigestSize()];
                        dig.DoFinal(result, 0);
                        return result;
                    }
                case AOSignConstants.SIGN_ALGORITHM_MD5:
                    {
                        MD5Digest dig = new MD5Digest();
                        dig.BlockUpdate(data, 0, data.Length);
                        byte[] result = new byte[dig.GetDigestSize()];
                        dig.DoFinal(result, 0);
                        return result;
                    }

                case AOSignConstants.SIGN_ALGORITHM_MD2:
                    {
                        MD2Digest dig = new MD2Digest();
                        dig.BlockUpdate(data, 0, data.Length);
                        byte[] result = new byte[dig.GetDigestSize()];
                        dig.DoFinal(result, 0);
                        return result;
                    }

                default:
                    // You can use the default case.
                    throw new ArgumentNullException("El algoritmo no es reconocido");
            }

            throw new ArgumentNullException("Algoritmo de hash no soportado: " + algo);
        }
Ejemplo n.º 3
0
 public Sha384Digest(Sha384Digest t)
     : base(t)
 {
 }
Ejemplo n.º 4
0
        public ITestResult Perform()
        {
            IDigest digest = new Sha384Digest();
            byte[] resBuf = new byte[digest.GetDigestSize()];
            string resStr;

            //
            // test 1
            //
            digest.DoFinal(resBuf, 0);

            resStr = Hex.ToHexString(resBuf);
            if (!resVec1.Equals(resStr))
            {
                return new SimpleTestResult(false,
                    "SHA-384 failing standard vector test 1"
                    + SimpleTest.NewLine
                    + "    expected: " + resVec1
                    + SimpleTest.NewLine
                    + "    got     : " + resStr);
            }

            //
            // test 2
            //
            byte[] bytes = Hex.Decode(testVec2);
            digest.BlockUpdate(bytes, 0, bytes.Length);

            digest.DoFinal(resBuf, 0);

            resStr = Hex.ToHexString(resBuf);
            if (!resVec2.Equals(resStr))
            {
                return new SimpleTestResult(false,
                    "SHA-384 failing standard vector test 2"
                    + SimpleTest.NewLine
                    + "    expected: " + resVec2
                    + SimpleTest.NewLine
                    + "    got     : " + resStr);
            }

            //
            // test 3
            //
            bytes = Hex.Decode(testVec3);

            digest.BlockUpdate(bytes, 0, bytes.Length);

            digest.DoFinal(resBuf, 0);

            resStr = Hex.ToHexString(resBuf);
            if (!resVec3.Equals(resStr))
            {
                return new SimpleTestResult(false,
                    "SHA-384 failing standard vector test 3"
                    + SimpleTest.NewLine
                    + "    expected: " + resVec3
                    + SimpleTest.NewLine
                    + "    got     : " + resStr);
            }

            //
            // test 4
            //
            bytes = Hex.Decode(testVec4);

            digest.BlockUpdate(bytes, 0, bytes.Length);

            digest.DoFinal(resBuf, 0);

            resStr = Hex.ToHexString(resBuf);
            if (!resVec4.Equals(resStr))
            {
                return new SimpleTestResult(false,
                    "SHA-384 failing standard vector test 4"
                    + SimpleTest.NewLine
                    + "    expected: " + resVec4
                    + SimpleTest.NewLine
                    + "    got     : " + resStr);
            }

            //
            // test 5
            //
            bytes = Hex.Decode(testVec4);

            digest.BlockUpdate(bytes, 0, bytes.Length/2);

            // clone the IDigest
            IDigest d = new Sha384Digest((Sha384Digest)digest);

            digest.BlockUpdate(bytes, bytes.Length/2, bytes.Length - bytes.Length/2);
            digest.DoFinal(resBuf, 0);

            resStr = Hex.ToHexString(resBuf);
            if (!resVec4.Equals(resStr))
            {
                return new SimpleTestResult(false,
                    "SHA384 failing standard vector test 5"
                    + SimpleTest.NewLine
                    + "    expected: " + resVec4
                    + SimpleTest.NewLine
                    + "    got     : " + resStr);
            }

            d.BlockUpdate(bytes, bytes.Length/2, bytes.Length - bytes.Length/2);
            d.DoFinal(resBuf, 0);

            resStr = Hex.ToHexString(resBuf);
            if (!resVec4.Equals(resStr))
            {
                return new SimpleTestResult(false,
                    "SHA384 failing standard vector test 5"
                    + SimpleTest.NewLine
                    + "    expected: " + resVec4
                    + SimpleTest.NewLine
                    + "    got     : " + resStr);
            }

            // test 6
            bytes = Hex.Decode(testVec5);
            for ( int i = 0; i < 100000; i++ )
            {
                digest.BlockUpdate(bytes, 0, bytes.Length);
            }
            digest.DoFinal(resBuf, 0);

            resStr = Hex.ToHexString(resBuf);
            if (!resVec5.Equals(resStr))
            {
                return new SimpleTestResult(false,
                    "SHA-384 failing standard vector test 5"
                    + SimpleTest.NewLine
                    + "    expected: " + resVec5
                    + SimpleTest.NewLine
                    + "    got     : " + resStr);
            }

            return new SimpleTestResult(true, Name + ": Okay");
        }
Ejemplo n.º 5
0
        /**
         * Copy constructor.  This will copy the state of the provided
         * message digest.
         */
        public Sha384Digest(
			Sha384Digest t)
			: base(t)
		{
		}
Ejemplo n.º 6
0
		/// <summary>
		/// Return the sha384 hash of the stream.
		/// </summary>
		/// <param name="instream">Data to be hashed.</param>
		public static string Sha384(Stream instream)
		{
			Sha384Digest digest = new Sha384Digest();
			return Encode(instream, digest);
		}
Ejemplo n.º 7
0
		/// <summary>
		/// Return the sha384 hash of the byte array.
		/// </summary>
		/// <param name="data">Data to be hashed.</param>
		public static string Sha384(byte[] data)
		{
			Sha384Digest digest = new Sha384Digest();
			return Encode(data, digest);
		}
Ejemplo n.º 8
0
		private IDigest GetDigest(THashAlgorithm hashAlgorithm)
		{
			IDigest result = null;
			switch (hashAlgorithm)
			{
				case THashAlgorithm.None:
					result = new NullDigest();
					break;
				case THashAlgorithm.MD5:
					result = new MD5Digest();
					break;
				case THashAlgorithm.SHA1:
					result = new Sha1Digest();
					break;
				case THashAlgorithm.SHA224:
					result = new Sha224Digest();
					break;
				case THashAlgorithm.SHA256:
					result = new Sha256Digest();
					break;
				case THashAlgorithm.SHA384:
					result = new Sha384Digest();
					break;
				case THashAlgorithm.SHA512:
					result = new Sha512Digest();
					break;
				default:
					break;
			}
			return result;
		}
        /// <summary>
        /// Returns implementation of specified hash algorithm
        /// </summary>
        /// <param name="hashAlgorithm">Hash algorithm</param>
        /// <returns>Implementation of specified hash algorithm</returns>
        private static IDigest GetHashGenerator(HashAlgorithm hashAlgorithm)
        {
            IDigest digest = null;

            switch (hashAlgorithm)
            {
                case HashAlgorithm.SHA1:
                    digest = new Sha1Digest();
                    break;
                case HashAlgorithm.SHA256:
                    digest = new Sha256Digest();
                    break;
                case HashAlgorithm.SHA384:
                    digest = new Sha384Digest();
                    break;
                case HashAlgorithm.SHA512:
                    digest = new Sha512Digest();
                    break;
                default:
                    throw new NotSupportedException("Unsupported hash algorithm");
            }

            return digest;
        }
Ejemplo n.º 10
0
		/// <summary>
		///   Creates a new signing key pair
		/// </summary>
		/// <param name="name">The name of the key or zone</param>
		/// <param name="recordClass">The record class of the DnsKeyRecord</param>
		/// <param name="timeToLive">The TTL in seconds to the DnsKeyRecord</param>
		/// <param name="flags">The Flags of the DnsKeyRecord</param>
		/// <param name="protocol">The protocol version</param>
		/// <param name="algorithm">The key algorithm</param>
		/// <param name="keyStrength">The key strength or 0 for default strength</param>
		/// <returns></returns>
		public static DnsKeyRecord CreateSigningKey(DomainName name, RecordClass recordClass, int timeToLive, DnsKeyFlags flags, byte protocol, DnsSecAlgorithm algorithm, int keyStrength = 0)
		{
			byte[] privateKey;
			byte[] publicKey;

			switch (algorithm)
			{
				case DnsSecAlgorithm.RsaSha1:
				case DnsSecAlgorithm.RsaSha1Nsec3Sha1:
				case DnsSecAlgorithm.RsaSha256:
				case DnsSecAlgorithm.RsaSha512:
					if (keyStrength == 0)
						keyStrength = (flags == (DnsKeyFlags.Zone | DnsKeyFlags.SecureEntryPoint)) ? 2048 : 1024;

					RsaKeyPairGenerator rsaKeyGen = new RsaKeyPairGenerator();
					rsaKeyGen.Init(new KeyGenerationParameters(_secureRandom, keyStrength));
					var rsaKey = rsaKeyGen.GenerateKeyPair();
					privateKey = PrivateKeyInfoFactory.CreatePrivateKeyInfo(rsaKey.Private).GetDerEncoded();
					var rsaPublicKey = (RsaKeyParameters) rsaKey.Public;
					var rsaExponent = rsaPublicKey.Exponent.ToByteArrayUnsigned();
					var rsaModulus = rsaPublicKey.Modulus.ToByteArrayUnsigned();

					int offset = 1;
					if (rsaExponent.Length > 255)
					{
						publicKey = new byte[3 + rsaExponent.Length + rsaModulus.Length];
						DnsMessageBase.EncodeUShort(publicKey, ref offset, (ushort) publicKey.Length);
					}
					else
					{
						publicKey = new byte[1 + rsaExponent.Length + rsaModulus.Length];
						publicKey[0] = (byte) rsaExponent.Length;
					}
					DnsMessageBase.EncodeByteArray(publicKey, ref offset, rsaExponent);
					DnsMessageBase.EncodeByteArray(publicKey, ref offset, rsaModulus);
					break;

				case DnsSecAlgorithm.Dsa:
				case DnsSecAlgorithm.DsaNsec3Sha1:
					if (keyStrength == 0)
						keyStrength = 1024;

					DsaParametersGenerator dsaParamsGen = new DsaParametersGenerator();
					dsaParamsGen.Init(keyStrength, 12, _secureRandom);
					DsaKeyPairGenerator dsaKeyGen = new DsaKeyPairGenerator();
					dsaKeyGen.Init(new DsaKeyGenerationParameters(_secureRandom, dsaParamsGen.GenerateParameters()));
					var dsaKey = dsaKeyGen.GenerateKeyPair();
					privateKey = PrivateKeyInfoFactory.CreatePrivateKeyInfo(dsaKey.Private).GetDerEncoded();
					var dsaPublicKey = (DsaPublicKeyParameters) dsaKey.Public;

					var dsaY = dsaPublicKey.Y.ToByteArrayUnsigned();
					var dsaP = dsaPublicKey.Parameters.P.ToByteArrayUnsigned();
					var dsaQ = dsaPublicKey.Parameters.Q.ToByteArrayUnsigned();
					var dsaG = dsaPublicKey.Parameters.G.ToByteArrayUnsigned();
					var dsaT = (byte) ((dsaY.Length - 64) / 8);

					publicKey = new byte[21 + 3 * dsaY.Length];
					publicKey[0] = dsaT;
					dsaQ.CopyTo(publicKey, 1);
					dsaP.CopyTo(publicKey, 21);
					dsaG.CopyTo(publicKey, 21 + dsaY.Length);
					dsaY.CopyTo(publicKey, 21 + 2 * dsaY.Length);
					break;

				case DnsSecAlgorithm.EccGost:
					ECDomainParameters gostEcDomainParameters = ECGost3410NamedCurves.GetByOid(CryptoProObjectIdentifiers.GostR3410x2001CryptoProA);

					var gostKeyGen = new ECKeyPairGenerator();
					gostKeyGen.Init(new ECKeyGenerationParameters(gostEcDomainParameters, _secureRandom));

					var gostKey = gostKeyGen.GenerateKeyPair();
					privateKey = PrivateKeyInfoFactory.CreatePrivateKeyInfo(gostKey.Private).GetDerEncoded();
					var gostPublicKey = (ECPublicKeyParameters) gostKey.Public;

					publicKey = new byte[64];

					gostPublicKey.Q.X.ToBigInteger().ToByteArrayUnsigned().CopyTo(publicKey, 32);
					gostPublicKey.Q.Y.ToBigInteger().ToByteArrayUnsigned().CopyTo(publicKey, 0);

					publicKey = publicKey.Reverse().ToArray();
					break;

				case DnsSecAlgorithm.EcDsaP256Sha256:
				case DnsSecAlgorithm.EcDsaP384Sha384:
					int ecDsaDigestSize;
					X9ECParameters ecDsaCurveParameter;

					if (algorithm == DnsSecAlgorithm.EcDsaP256Sha256)
					{
						ecDsaDigestSize = new Sha256Digest().GetDigestSize();
						ecDsaCurveParameter = NistNamedCurves.GetByOid(SecObjectIdentifiers.SecP256r1);
					}
					else
					{
						ecDsaDigestSize = new Sha384Digest().GetDigestSize();
						ecDsaCurveParameter = NistNamedCurves.GetByOid(SecObjectIdentifiers.SecP384r1);
					}

					ECDomainParameters ecDsaP384EcDomainParameters = new ECDomainParameters(
						ecDsaCurveParameter.Curve,
						ecDsaCurveParameter.G,
						ecDsaCurveParameter.N,
						ecDsaCurveParameter.H,
						ecDsaCurveParameter.GetSeed());

					var ecDsaKeyGen = new ECKeyPairGenerator();
					ecDsaKeyGen.Init(new ECKeyGenerationParameters(ecDsaP384EcDomainParameters, _secureRandom));

					var ecDsaKey = ecDsaKeyGen.GenerateKeyPair();
					privateKey = PrivateKeyInfoFactory.CreatePrivateKeyInfo(ecDsaKey.Private).GetDerEncoded();
					var ecDsaPublicKey = (ECPublicKeyParameters) ecDsaKey.Public;

					publicKey = new byte[ecDsaDigestSize * 2];

					ecDsaPublicKey.Q.X.ToBigInteger().ToByteArrayUnsigned().CopyTo(publicKey, 0);
					ecDsaPublicKey.Q.Y.ToBigInteger().ToByteArrayUnsigned().CopyTo(publicKey, ecDsaDigestSize);
					break;

				default:
					throw new NotSupportedException();
			}

			return new DnsKeyRecord(name, recordClass, timeToLive, flags, protocol, algorithm, publicKey, privateKey);
		}