/// <summary>
 /// Signature hash
 /// </summary>
 /// <param name="data"></param>
 /// <param name="seed"></param>
 /// <param name="types"></param>
 /// <param name="preference"></param>
 /// <param name="managed"></param>
 /// <returns></returns>
 public static byte[] SignatureHash(byte[] data, uint seed,
                                    MurmurHash3Types types           = MurmurHash3Types.FAST,
                                    MurmurHash3Preference preference = MurmurHash3Preference.AUTO,
                                    MurmurHash3Managed managed       = MurmurHash3Managed.TRUE)
 {
     return(BitConverter.GetBytes(Signature(data, seed, types, preference, managed)));
 }
 /// <summary>
 /// Signature
 /// </summary>
 /// <param name="data"></param>
 /// <param name="seed"></param>
 /// <param name="types"></param>
 /// <param name="preference"></param>
 /// <param name="managed"></param>
 /// <returns></returns>
 public static uint Signature(byte[] data, uint seed,
                              MurmurHash3Types types           = MurmurHash3Types.FAST,
                              MurmurHash3Preference preference = MurmurHash3Preference.AUTO,
                              MurmurHash3Managed managed       = MurmurHash3Managed.TRUE)
 {
     Checker.Buffer(data);
     return(SignatureCore(data, seed, types, preference, managed));
 }
        /// <summary>
        /// Signature
        /// </summary>
        /// <param name="data"></param>
        /// <param name="seed"></param>
        /// <param name="encoding"></param>
        /// <param name="types"></param>
        /// <param name="preference"></param>
        /// <param name="managed"></param>
        /// <returns></returns>
        public static uint Signature(string data, uint seed, Encoding encoding = null,
                                     MurmurHash3Types types           = MurmurHash3Types.FAST,
                                     MurmurHash3Preference preference = MurmurHash3Preference.AUTO,
                                     MurmurHash3Managed managed       = MurmurHash3Managed.TRUE)
        {
            Checker.Data(data);

            var bytes = encoding.SafeValue().GetBytes(data);

            return(SignatureCore(bytes, seed, types, preference, managed));
        }
 /// <inheritdoc />
 public MurmurHash3InputStream(Stream underlyingStream, uint seed = 0,
                               MurmurHash3Managed managed         = MurmurHash3Managed.TRUE,
                               MurmurHash3Types types             = MurmurHash3Types.L_128,
                               MurmurHash3Preference preference   = MurmurHash3Preference.AUTO)
 {
     _underlyingStream = underlyingStream;
     _algorithm        = types switch
     {
         MurmurHash3Types.L_32 => (HashAlgorithm)MurmurHash3Core.CreateL32(seed, managed),
         MurmurHash3Types.L_128 => (HashAlgorithm)MurmurHash3Core.CreateL128(seed, managed, preference),
         _ => throw new InvalidOperationException("Invalid operation because only support L32 and L128")
     };
 }
Esempio n. 5
0
        /// <summary>
        /// Create MurmurHash3 32
        /// </summary>
        /// <param name="seed"></param>
        /// <param name="managed"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        internal static MurmurHash3L32 CreateL32(uint seed, MurmurHash3Managed managed)
        {
            switch (managed)
            {
            case MurmurHash3Managed.TRUE:
                return(new MurmurHash3L32ManagedX86(seed));

            case MurmurHash3Managed.FALSE:
                return(new MurmurHash3L32UnmanagedX86(seed));

            default:
                throw new InvalidOperationException("Unknown MurmurHash3 L32 managed mode");
            }
        }
Esempio n. 6
0
        internal static MurmurHash3L128 CreateL128(uint seed, MurmurHash3Managed managed, MurmurHash3Preference preference)
        {
            var algorithm = managed switch
            {
                MurmurHash3Managed.TRUE => __pick(s => new MurmurHash3L128ManagedX86(s), s => new MurmurHash3L128ManagedX64(s)),
                MurmurHash3Managed.FALSE => __pick(s => new MurmurHash3L128UnmanagedX86(s), s => new MurmurHash3L128UnmanagedX64(s)),
                _ => throw new InvalidOperationException("Unknown managed type.")
            };

            return(algorithm as MurmurHash3L128);

            HashAlgorithm __pick <T32, T64>(Func <uint, T32> __factory32, Func <uint, T64> __factory64)
                where T32 : HashAlgorithm where T64 : HashAlgorithm
            {
                switch (preference)
                {
                case MurmurHash3Preference.X64:
                    return(__factory64(seed));

                case MurmurHash3Preference.X86:
                    return(__factory32(seed));

                default:
                    if (__is64BitProcess())
                    {
                        return(__factory64(seed));
                    }
                    return(__factory32(seed));
                }
            }

            bool __is64BitProcess()
            {
#if NET451
                return(Environment.Is64BitProcess);
#elif NET40 || NET35
                return(IntPtr.Size == 8);
#else
                return(false);
#endif
            }
        }
    }
        private static uint SignatureCore(byte[] data, uint seed, MurmurHash3Types types, MurmurHash3Preference preference, MurmurHash3Managed managed)
        {
            switch (types)
            {
            case MurmurHash3Types.FAST:
            {
                return(MurmurHash3Core.FastMode.Hash32(data.AsSpan(), seed));
            }

            case MurmurHash3Types.L_32:
            {
                var l32 = MurmurHash3Core.CreateL32(seed, managed);
                var h32 = l32.ComputeHash(data);
                return(BitConverter.ToUInt32(h32, 0));
            }

            case MurmurHash3Types.L_128:
            {
                var l128 = MurmurHash3Core.CreateL128(seed, managed, preference);
                var h128 = l128.ComputeHash(data);
                return(BitConverter.ToUInt32(h128, 0));
            }

            default:
                throw new NotImplementedException("Unknown type for MurmurHash3 hash provider.");
            }
        }
 /// <summary>
 /// Verify
 /// </summary>
 /// <param name="comparison"></param>
 /// <param name="data"></param>
 /// <param name="seed"></param>
 /// <param name="types"></param>
 /// <param name="preference"></param>
 /// <param name="managed"></param>
 /// <returns></returns>
 public static bool Verify(uint comparison, byte[] data, uint seed,
                           MurmurHash3Types types           = MurmurHash3Types.FAST,
                           MurmurHash3Preference preference = MurmurHash3Preference.AUTO,
                           MurmurHash3Managed managed       = MurmurHash3Managed.TRUE)
 => comparison == Signature(data, seed, types, preference, managed);
 /// <summary>
 /// Verify
 /// </summary>
 /// <param name="comparison"></param>
 /// <param name="data"></param>
 /// <param name="seed"></param>
 /// <param name="encoding"></param>
 /// <param name="types"></param>
 /// <param name="preference"></param>
 /// <param name="managed"></param>
 /// <returns></returns>
 public static bool Verify(uint comparison, string data, uint seed, Encoding encoding = null,
                           MurmurHash3Types types           = MurmurHash3Types.FAST,
                           MurmurHash3Preference preference = MurmurHash3Preference.AUTO,
                           MurmurHash3Managed managed       = MurmurHash3Managed.TRUE)
 => comparison == Signature(data, seed, encoding, types, preference, managed);
 /// <summary>
 /// Create a new instance of <see cref="MurmurHash3OutputStream"/>.
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="seed"></param>
 /// <param name="managed"></param>
 /// <param name="types"></param>
 /// <param name="preference"></param>
 /// <returns></returns>
 public static MurmurHash3OutputStream CreateOutputStream(Stream stream, uint seed         = 0,
                                                          MurmurHash3Managed managed       = MurmurHash3Managed.TRUE,
                                                          MurmurHash3Types types           = MurmurHash3Types.L_128,
                                                          MurmurHash3Preference preference = MurmurHash3Preference.AUTO)
 => new MurmurHash3OutputStream(stream, seed, managed, types, preference);