public void TestToTrapMessageChinese()
        {
            Variable v = new Variable(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }),
                                      new OctetString("ол╣Щ", Encoding.Unicode));
            List <Variable> vList = new List <Variable>();

            vList.Add(v);

            TrapV1Pdu pdu = new TrapV1Pdu(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }),
                                          new IP("127.0.0.1"),
                                          new Integer32((int)GenericCode.EnterpriseSpecific),
                                          new Integer32(12),
                                          new TimeTicks(16352),
                                          vList);

            byte[]        bytes   = SnmpMessageExtension.PackMessage(VersionCode.V1, new OctetString("public"), pdu).ToBytes();
            TrapV1Message message = (TrapV1Message)MessageFactory.ParseMessages(bytes, new UserRegistry())[0];

            Assert.AreEqual("127.0.0.1", message.AgentAddress.ToString());
            Assert.AreEqual(GenericCode.EnterpriseSpecific, message.Generic);
            Assert.AreEqual(12, message.Specific);
            Assert.AreEqual(16352, message.TimeStamp);
            Assert.AreEqual(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, message.Enterprise.ToNumerical());
            Assert.AreEqual(1, message.Variables.Count);
            Assert.AreEqual(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }, message.Variables[0].Id.ToNumerical());
            Assert.AreEqual("ол╣Щ", ((OctetString)message.Variables[0].Data).ToString(Encoding.Unicode));
        }
        public void TestToTrapMessage()
        {
            Variable v = new Variable(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }),
                                      new OctetString("TrapTest"));
            List <Variable> vList = new List <Variable> {
                v
            };

            TrapV1Pdu pdu = new TrapV1Pdu(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 },
                                          new IP("127.0.0.1"),
                                          new Integer32((int)GenericCode.EnterpriseSpecific),
                                          new Integer32(12),
                                          new TimeTicks(16352),
                                          vList);

            byte[]        bytes   = SnmpMessageExtension.PackMessage(VersionCode.V1, new OctetString("public"), pdu).ToBytes();
            TrapV1Message message = (TrapV1Message)MessageFactory.ParseMessages(bytes, new UserRegistry())[0];

            Assert.AreEqual("127.0.0.1", message.AgentAddress.ToString());
            Assert.AreEqual(GenericCode.EnterpriseSpecific, message.Generic);
            Assert.AreEqual(12, message.Specific);
            Assert.AreEqual(16352, message.TimeStamp);
            Assert.AreEqual(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, message.Enterprise.ToNumerical());
            Assert.AreEqual(1, message.Variables().Count);
            Assert.AreEqual(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }, message.Variables()[0].Id.ToNumerical());
            Assert.AreEqual("TrapTest", message.Variables()[0].Data.ToString());
            Assert.AreEqual("SNMPv1 TRAP PDU: agent address: 127.0.0.1; time stamp: 16352 (00:02:43.5200000); enterprise: .1.3.6.1.4.1.2162.1000.2; generic: EnterpriseSpecific; specific: 12; varbind count: 1", pdu.ToString());
        }
Exemple #3
0
        /// <summary>
        /// Computes the hash.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="header">The header.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="data">The scope bytes.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <returns></returns>
        public OctetString ComputeHash(VersionCode version, ISegment header, SecurityParameters parameters, ISnmpData data, IPrivacyProvider privacy)
        {
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }

            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            if (privacy == null)
            {
                throw new ArgumentNullException("privacy");
            }

            var key = PasswordToKey(_password, parameters.EngineId.GetRaw());

            using (var sha1 = new HMACSHA1(key))
            {
                var hash = sha1.ComputeHash(SnmpMessageExtension.PackMessage(version, header, parameters, data).ToBytes());
                sha1.Clear();
                var result = new byte[DigestLength];
                Buffer.BlockCopy(hash, 0, result, 0, result.Length);
                return(new OctetString(result));
            }
        }
        /// <summary>
        /// Computes the hash.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="header">The header.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="scopeBytes">The scope bytes.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <returns></returns>
        private OctetString ComputeHash(VersionCode version, ISegment header, SecurityParameters parameters, ISnmpData scopeBytes, IPrivacyProvider privacy)
        {
            if (scopeBytes == null)
            {
                throw new ArgumentNullException("scopeBytes");
            }

            byte[] key = PasswordToKey(_password, parameters.EngineId.GetRaw());
            using (HMACSHA1 sha1 = new HMACSHA1(key))
            {
                byte[] hash = sha1.ComputeHash(SnmpMessageExtension.PackMessage(version, header, parameters, scopeBytes, privacy).ToBytes());
                sha1.Clear();
                byte[] result = new byte[DigestLength];
                Array.Copy(hash, result, result.Length);
                return(new OctetString(result));
            }
        }