DigestUtilities.cs :  » PDF » iTextSharp » Org » BouncyCastle » Security » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » iTextSharp 
iTextSharp » Org » BouncyCastle » Security » DigestUtilities.cs
using System.Collections;
using System.Globalization;

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.TeleTrust;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto;

namespace Org.BouncyCastle.Security{
    /// <remarks>
    ///  Utility class for creating IDigest objects from their names/Oids
    /// </remarks>
    public sealed class DigestUtilities
    {
    private DigestUtilities()
    {
    }

        private static readonly Hashtable algorithms = new Hashtable();
        private static readonly Hashtable oids = new Hashtable();

        static DigestUtilities()
        {
            algorithms[PkcsObjectIdentifiers.MD2.Id] = "MD2";
            algorithms[PkcsObjectIdentifiers.MD4.Id] = "MD4";
            algorithms[PkcsObjectIdentifiers.MD5.Id] = "MD5";

            algorithms["SHA1"] = "SHA-1";
            algorithms[OiwObjectIdentifiers.IdSha1.Id] = "SHA-1";
            algorithms["SHA224"] = "SHA-224";
            algorithms[NistObjectIdentifiers.IdSha224.Id] = "SHA-224";
            algorithms["SHA256"] = "SHA-256";
            algorithms[NistObjectIdentifiers.IdSha256.Id] = "SHA-256";
            algorithms["SHA384"] = "SHA-384";
            algorithms[NistObjectIdentifiers.IdSha384.Id] = "SHA-384";
            algorithms["SHA512"] = "SHA-512";
            algorithms[NistObjectIdentifiers.IdSha512.Id] = "SHA-512";

            algorithms["RIPEMD-128"] = "RIPEMD128";
            algorithms[TeleTrusTObjectIdentifiers.RipeMD128.Id] = "RIPEMD128";
            algorithms["RIPEMD-160"] = "RIPEMD160";
            algorithms[TeleTrusTObjectIdentifiers.RipeMD160.Id] = "RIPEMD160";
            algorithms["RIPEMD-256"] = "RIPEMD256";
            algorithms[TeleTrusTObjectIdentifiers.RipeMD256.Id] = "RIPEMD256";
      algorithms["RIPEMD-320"] = "RIPEMD320";
//      algorithms[TeleTrusTObjectIdentifiers.RipeMD320.Id] = "RIPEMD320";

      algorithms[CryptoProObjectIdentifiers.GostR3411.Id] = "GOST3411";



            oids["MD2"] = PkcsObjectIdentifiers.MD2;
            oids["MD4"] = PkcsObjectIdentifiers.MD4;
            oids["MD5"] = PkcsObjectIdentifiers.MD5;
            oids["SHA-1"] = OiwObjectIdentifiers.IdSha1;
            oids["SHA-224"] = NistObjectIdentifiers.IdSha224;
            oids["SHA-256"] = NistObjectIdentifiers.IdSha256;
            oids["SHA-384"] = NistObjectIdentifiers.IdSha384;
            oids["SHA-512"] = NistObjectIdentifiers.IdSha512;
            oids["RIPEMD128"] = TeleTrusTObjectIdentifiers.RipeMD128;
            oids["RIPEMD160"] = TeleTrusTObjectIdentifiers.RipeMD160;
            oids["RIPEMD256"] = TeleTrusTObjectIdentifiers.RipeMD256;
      oids["GOST3411"] = CryptoProObjectIdentifiers.GostR3411;
        }

    /// <summary>
        /// Returns a ObjectIdentifier for a given digest mechanism.
        /// </summary>
        /// <param name="mechanism">A string representation of the digest meanism.</param>
        /// <returns>A DerObjectIdentifier, null if the Oid is not available.</returns>

        public static DerObjectIdentifier GetObjectIdentifier(
      string mechanism)
        {
      if (mechanism == null)
        throw new System.ArgumentNullException("mechanism");

      mechanism = mechanism.ToUpper(CultureInfo.InvariantCulture);
      string aliased = (string) algorithms[mechanism];

      if (aliased != null)
        mechanism = aliased;

      return (DerObjectIdentifier) oids[mechanism];
        }

        public static ICollection Algorithms
        {
      get { return oids.Keys; }
        }

        public static IDigest GetDigest(
      DerObjectIdentifier id)
        {
            return GetDigest(id.Id);
        }

        public static IDigest GetDigest(
      string algorithm)
        {
      string upper = algorithm.ToUpper(CultureInfo.InvariantCulture);
            string mechanism = (string) algorithms[upper];

      if (mechanism == null)
      {
        mechanism = upper;
      }

      switch (mechanism)
      {
        case "GOST3411":  return new Gost3411Digest();
        case "MD2":      return new MD2Digest();
        case "MD4":      return new MD4Digest();
        case "MD5":      return new MD5Digest();
        case "RIPEMD128":  return new RipeMD128Digest();
        case "RIPEMD160":  return new RipeMD160Digest();
        case "RIPEMD256":  return new RipeMD256Digest();
        case "RIPEMD320":  return new RipeMD320Digest();
        case "SHA-1":    return new Sha1Digest();
        case "SHA-224":    return new Sha224Digest();
        case "SHA-256":    return new Sha256Digest();
        case "SHA-384":    return new Sha384Digest();
        case "SHA-512":    return new Sha512Digest();
        case "TIGER":    return new TigerDigest();
        case "WHIRLPOOL":  return new WhirlpoolDigest();
        default:
          throw new SecurityUtilityException("Digest " + mechanism + " not recognised.");
      }
        }

    public static string GetAlgorithmName(
      DerObjectIdentifier oid)
        {
            return (string) algorithms[oid.Id];
        }

    public static byte[] CalculateDigest(string algorithm, byte[] input)
    {
      IDigest digest = GetDigest(algorithm);
      digest.BlockUpdate(input, 0, input.Length);
      return DoFinal(digest);
    }

    public static byte[] DoFinal(
      IDigest digest)
    {
      byte[] b = new byte[digest.GetDigestSize()];
      digest.DoFinal(b, 0);
      return b;
    }

    public static byte[] DoFinal(
      IDigest  digest,
        byte[]  input)
    {
      digest.BlockUpdate(input, 0, input.Length);
      return DoFinal(digest);
    }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.