Arrays.cs :  » PDF » iTextSharp » Org » BouncyCastle » Utilities » 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 » Utilities » Arrays.cs
using System;
using System.Text;

namespace Org.BouncyCastle.Utilities{

    /// <summary> General array utilities.</summary>
    public sealed class Arrays
    {
        private Arrays()
        {
        }

    public static bool AreEqual(
      bool[]  a,
      bool[]  b)
    {
      if (a == b)
        return true;

      if (a == null || b == null)
        return false;

      int i = a.Length;
      if (i != b.Length)
        return false;
      while (i != 0)
      {
        --i;
        if (a[i] != b[i])
          return false;
      }
      return true;
    }

        /// <summary>
        /// Are two arrays equal.
        /// </summary>
        /// <param name="a">Left side.</param>
        /// <param name="b">Right side.</param>
        /// <returns>True if equal.</returns>
        public static bool AreEqual(
      byte[]  a,
      byte[]  b)
        {
      if (a == b)
        return true;

      if (a == null || b == null)
        return false;

      return HaveSameContents(a, b);
    }

    [Obsolete("Use 'AreEqual' method instead")]
    public static bool AreSame(
      byte[]  a,
      byte[]  b)
    {
      return AreEqual(a, b);
    }

    /// <summary>
    /// A constant time equals comparison - does not terminate early if
    /// test will fail.
    /// </summary>
    /// <param name="a">first array</param>
    /// <param name="b">second array</param>
    /// <returns>true if arrays equal, false otherwise.</returns>
    public static bool ConstantTimeAreEqual(
      byte[]  a,
      byte[]  b)
    {
      int i = a.Length;
      if (i != b.Length)
        return false;
      int cmp = 0;
      while (i != 0)
      {
        --i;
        cmp |= (a[i] ^ b[i]);
      }
      return cmp == 0;
    }

    public static bool AreEqual(
      int[]  a,
      int[]  b)
    {
      if (a == b)
        return true;

      if (a == null || b == null)
        return false;

      return HaveSameContents(a, b);
    }

    private static bool HaveSameContents(
      byte[]  a,
      byte[]  b)
    {
      int i = a.Length;
      if (i != b.Length)
        return false;
      while (i != 0)
      {
        --i;
        if (a[i] != b[i])
          return false;
      }
      return true;
    }

    private static bool HaveSameContents(
      int[]  a,
      int[]  b)
    {
      int i = a.Length;
      if (i != b.Length)
        return false;
      while (i != 0)
      {
        --i;
        if (a[i] != b[i])
          return false;
      }
      return true;
    }

    public static string ToString(
      object[] a)
    {
      StringBuilder sb = new StringBuilder('[');
      if (a.Length > 0)
      {
        sb.Append(a[0]);
        for (int index = 1; index < a.Length; ++index)
        {
          sb.Append(", ").Append(a[index]);
        }
      }
      sb.Append(']');
      return sb.ToString();
    }

    public static int GetHashCode(
      byte[] data)
    {
      if (data == null)
      {
        return 0;
      }

      int i = data.Length;
      int hc = i + 1;

      while (--i >= 0)
      {
        hc *= 257;
        hc ^= data[i];
      }

      return hc;
    }

    public static byte[] Clone(
      byte[] data)
    {
      return data == null ? null : (byte[]) data.Clone();
    }

    public static int[] Clone(
      int[] data)
    {
      return data == null ? null : (int[]) data.Clone();
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.