public static void VerifySmartCard(SmartCardDevice smartCardDevice, byte[] com, byte[] response, string hashFunctionName, byte[] proofSession, byte[] challangeMgs) { BigInteger resp = new BigInteger(1, response); //BigInteger comBig = new BigInteger(1, com); HashFunction hash = new HashFunction(hashFunctionName); //hash.Hash(new byte[1] {0x1}); //hash.Hash(1); byte[] proofSessionFoo = new byte[1 + proofSession.Length]; proofSessionFoo[0] = 1; Buffer.BlockCopy(proofSession, 0, proofSessionFoo, 1, proofSession.Length); hash.Hash(proofSessionFoo); hash.Hash(challangeMgs); byte[] cByte = hash.Digest; BigInteger c = new BigInteger(1, cByte); byte[] devicePubKeyByte = smartCardDevice.Device.GetDevicePublicKey(true); //BigInteger devicePubKey = new BigInteger(1, devicePubKeyByte); SubgroupGroupDescription subGq = (SubgroupGroupDescription)smartCardDevice.getGroupDescription(); SubgroupGroupElement leftSide = (SubgroupGroupElement)smartCardDevice.getGroupElement().Exponentiate(resp); SubgroupGroupElement pk = (SubgroupGroupElement)subGq.CreateGroupElement(devicePubKeyByte); SubgroupGroupElement pkc = (SubgroupGroupElement)pk.Exponentiate(c.Negate()); SubgroupGroupElement rightSide = (SubgroupGroupElement)pkc.Multiply(smartCardDevice.getGroupDescription().CreateGroupElement(com)); Console.Out.WriteLine("Printing left and right side"); Utils.PrintByteArrayToConsole(leftSide.GetEncoded()); Utils.PrintByteArrayToConsole(rightSide.GetEncoded()); }
/// <summary> /// Creates a hash of the <paramref name="password"/> using the specified parameters. /// </summary> /// <param name="password">The string for which the hash is created.</param> /// <param name="hashFunction">The type of hash function to use to create the hash.</param> /// <param name="hashIterations">The number of key-stretching iterations to complete when creating the hash.</param> /// <param name="saltBytes">The salt for <paramref name="password"/> as an array of bytes.</param> /// <returns> /// The created hash of the <paramref name="password"/>. /// </returns> private static string Compute(string password, HashFunction hashFunction, Int32 hashIterations, byte[] saltBytes) { if (null == saltBytes) throw new ArgumentNullException("saltBytes"); // Gets the length of the salt as a byte array // so it can be added to the final computation. var saltLength = Convert.ToInt16(saltBytes.Length); var saltLengthBytes = BitConverter.GetBytes(saltLength); // Gets the type of hash function and the number // of iterations as byte arrays so they can be // added to the final computation. var hashFunctionBytes = BitConverter.GetBytes((short)hashFunction); var hashIterationBytes = BitConverter.GetBytes(hashIterations); // Converts the plain-text password into a byte array // and adds the specified salt. var passwordBytes = PasswordEncoding.GetBytes(password); var passwordAndSaltBytes = passwordBytes .Concat(saltBytes) .ToArray(); // Creates the hash algorithm that is used to generate // the password hash. var hashBytes = default(byte[]); using (var hashAlgorithm = CreateHashAlgorithm(hashFunction)) { // The hash is initialized by hashing the password // and salt. hashAlgorithm.Initialize(); hashBytes = hashAlgorithm.ComputeHash(passwordAndSaltBytes); // Performs key stretching over the specified number // of hash iterations. while (hashIterations-- > 0) { // The hash is modified during each iteration by // computing another hash of the previous hash // and the entered password. hashBytes = hashBytes.Concat(passwordAndSaltBytes).ToArray(); hashBytes = hashAlgorithm.ComputeHash(hashBytes); } // The hashing algorithm isn't needed anymore. hashAlgorithm.Clear(); } // Creates a single byte array of all the data // required to recreate the computation for the // given password. var computedResult = new byte[] { } .Concat(saltLengthBytes) .Concat(saltBytes) .Concat(hashFunctionBytes) .Concat(hashIterationBytes) .Concat(hashBytes) .ToArray(); // Return the data encoded as a string. return Convert.ToBase64String(computedResult); }
/// <summary> /// Creates hashtable of empty elements. /// </summary> /// <param name="hashsize"></param> public HashTable(int hashsize) { table = new List[hashsize]; for (int i = 0; i < hashsize; ++i) table[i] = new List(); HashSize = hashsize; this.countHash = this.CalculateHash; }
/// <summary> /// Creates new hashtable of empty elements, takes a function for the hash table. /// </summary> /// <param name="hashsize"></param> public HashTable(int hashsize, HashFunction CountHash) { table = new List[hashsize]; for (int i = 0; i < hashsize; ++i) table[i] = new List(); HashSize = hashsize; this.countHash = CountHash; }
/// <summary> /// Creates a new instance of <see cref="T:HashAlgorithm"/> for the specified <paramref name="hashFunction" />. /// </summary> /// <param name="hashFunction"> /// The type of hash algorithm to create. /// </param> /// <returns> /// A new instance of <see cref="T:HashAlgorithm"/> for the specified <paramref name="hashFunction" />. /// </returns> private static HashAlgorithm CreateHashAlgorithm(HashFunction hashFunction) { switch (hashFunction) { case HashFunction.SHA256: return SHA256.Create(); case HashFunction.SHA384: return SHA384.Create(); case HashFunction.SHA512: return SHA512.Create(); default: throw new ArgumentException( string.Format("The {0} value {1} has not been implemented.", typeof(HashFunction), hashFunction), "hashFunction" ); } }
public override int GetHashCode() { int hash = 1; if (HashFunction != 0) { hash ^= HashFunction.GetHashCode(); } if (ArchivePayload.Length != 0) { hash ^= ArchivePayload.GetHashCode(); } if (Hash.Length != 0) { hash ^= Hash.GetHashCode(); } if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } return(hash); }
public void Scenario1() { int testValue = 1; var function = new HashFunction(5, 0.05); var addingResult = function.Insert(testValue); Assert.IsTrue(addingResult); var memberResult = function.Member(testValue); Assert.IsTrue(memberResult); var removeResult = function.Delete(testValue); Assert.IsTrue(memberResult); var memberResult2 = function.Member(testValue); Assert.IsFalse(memberResult2); }
private void HashPerformance(HashFunction hashFunction) { string name = hashFunction.GetType().Name; int m = 1000; int k = 10; int count = 100000; var bf = new FilterMemory <string>(m, k, hashFunction); var array = new List <byte[]>(); for (int i = 0; i < 1000; i++) { array.Add(Helper.GenerateBytes(16)); } bf.Add(Helper.GenerateString(20)); Console.WriteLine(name + ":" + bf); int arrayCount = array.Count; Helper.Time(name, (n) => { bf.ComputeHash(array[n % arrayCount]); }, count); Helper.TimeWithThread($"{name} {Environment.ProcessorCount} Thread", (t, n) => { bf.ComputeHash(array[n % arrayCount]); }, Environment.ProcessorCount, count); Helper.TimeWithParallel($"{name} Parallel", (n) => { bf.ComputeHash(array[n % arrayCount]); }, count); }
public static long UpdateHash(HashFunction hashFunction, long hash, string value) { //NOTE that we are basing the hash code on code points instead of char[] values. int length = value.Length; int codePointCount = 0; for (int offset = 0; offset < length;) { int codePointA = char.ConvertToUtf32(value, offset); int codePointB = 0; offset += Character.charCount(codePointA); codePointCount++; if (offset < length) { codePointB = char.ConvertToUtf32(value, offset); offset += Character.charCount(codePointB); codePointCount++; } hash = hashFunction.Update(hash, (( long )codePointA << 32) + codePointB); } return(hashFunction.Update(hash, codePointCount)); }
public override int GetHashCode() { int hash = 1; if (HashFunction != global::Com.DigitalAsset.Daml_Lf_1_8.DamlLf.HashFunction.Sha256) { hash ^= HashFunction.GetHashCode(); } if (Payload.Length != 0) { hash ^= Payload.GetHashCode(); } if (Hash.Length != 0) { hash ^= Hash.GetHashCode(); } if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } return(hash); }
// [TestCase(5)] // [TestCase(6)] // [TestCase(7)] public void TestMultipleBitLengthsOfZz(int bitsToDrop) { var zz = new BitString("51020F0B4423F066FFA2B9CA5FA536603F307A52048D7FBE4FA2D0A8F5B6ACF116BE68A28F6D21C37A4A76F082A5CE0D240FB84D6E3291C9B49E6865E76AB166934C701DB25A7FF77DE8849657244CB818B813B72D404D86DC1B0CFF64AFABFEE0CE24DF6DD6C537B6E58DBBC96DD4B18DDD0583699234B82BDDF4778236DD0B944E042BE5DA951E4B7D4962086964D0929F3641AB403CE4FA0AB9F2E500A5004786153E3E7740C41EA0C396773EC2D5D0E98837372491B97C42A336AEA3CF1F13C090CB3F45BA1AC67D763D09B9279D4158A6B41FB8C0E8515473F882174184EDDD5FAECC043E779DA7F0080CE31DE03B255DE038ACEC636F6CDF7DCD47B2C6757310179952B29F48DF510C2C204520C2313034DDB7080DE4F9018EA37756FEA41C285213E0216B5A3BA730AAC15C60EB72E5143656E2ADD533F7CACE1CCAE0100BDD789130A3B755669BC064DD31ECB3CFAB6E5766FA7194C982B79776D1F5FC1C1DECBE102096E14C7B30C8876757EB9F419002C6FC2DE88B98B1C784F3C0976447DC352D8BF56FBE255394E2FD3D7B5A4C9B4770"); zz = zz.GetMostSignificantBits(zz.BitLength - bitsToDrop); var otherInfo = new BitString("82852BF64C6CF56399CC0E5F7ADAE2DF286B51E0D1A274BE949AF3D72A29CBB21F8AC17574BB12550804F4A8AA498082"); var hashFunction = new HashFunction(ModeValues.SHA1, DigestSizes.d160); var sha = _factory.GetShaInstance(hashFunction); var subject = new AnsiX942Concat(sha); var param = new ConcatAns942Parameters { Zz = zz, KeyLen = 384, OtherInfo = otherInfo }; var result = subject.DeriveKey(param); Assert.True(result.Success); Assert.AreEqual(new BitString("DD8469A101758AA949516B0AE4B8F6B56438A0A119575D3265163C6BBBEA2CB58629F387A1AA163D5AE164C5206E1DB5").ToHex(), result.DerivedKey.ToHex()); }
public void testHashCodesM3_128_double() { int seed = 123; Random rand = new Random(seed); HashFunction hf = Hashing.murmur3_128(seed); for (int i = 0; i < 1000; i++) { double val = rand.nextDouble(); byte[] data = ByteBuffer.allocate(8).putDouble(val).array(); // guava stores the hashcodes in little endian order ByteBuffer buf = ByteBuffer.allocate(16).order(ByteOrder.LITTLE_ENDIAN); buf.put(hf.hashBytes(data).asBytes()); buf.flip(); long gl1 = buf.getLong(); long gl2 = buf.getLong(8); long[] hc = Murmur3.hash128(data, 0, data.length, seed); long m1 = hc[0]; long m2 = hc[1]; Assert.Equal(gl1, m1); Assert.Equal(gl2, m2); } }
public void ShouldIkeV1Correctly(ModeValues mode, DigestSizes digestSize, string niHex, string nrHex, string gxyHex, string ckyiHex, string ckyrHex, string sKeyIdHex, string sKeyIdDHex, string sKeyIdAHex, string sKeyIdEHex) { var ni = new BitString(niHex); var nr = new BitString(nrHex); var gxy = new BitString(gxyHex); var ckyi = new BitString(ckyiHex); var ckyr = new BitString(ckyrHex); var sKeyId = new BitString(sKeyIdHex); var sKeyIdD = new BitString(sKeyIdDHex); var sKeyIdA = new BitString(sKeyIdAHex); var sKeyIdE = new BitString(sKeyIdEHex); var hash = new HashFunction(mode, digestSize); var subject = _factory.GetIkeV1Instance(AuthenticationMethods.Dsa, hash); var result = subject.GenerateIke(ni, nr, gxy, ckyi, ckyr); Assert.IsTrue(result.Success); Assert.AreEqual(sKeyId, result.SKeyId, "sKeyId"); Assert.AreEqual(sKeyIdD, result.SKeyIdD, "sKeyIdD"); Assert.AreEqual(sKeyIdA, result.SKeyIdA, "sKeyIdA"); Assert.AreEqual(sKeyIdE, result.SKeyIdE, "sKeyIdE"); }
public void Test_ECDSA(CurvePrimeField Curve1, CurvePrimeField Curve2, HashFunction HashFunction) { int n; using (RandomNumberGenerator rnd = RandomNumberGenerator.Create()) { for (n = 0; n < 100; n++) { byte[] Data = new byte[1024]; rnd.GetBytes(Data); KeyValuePair <BigInteger, BigInteger> Signature = Curve1.Sign(Data, HashFunction); bool Valid = Curve2.Verify(Data, Curve1.PublicKey, HashFunction, Signature); Assert.IsTrue(Valid); Curve1.GenerateKeys(); Curve2.GenerateKeys(); } } }
/// <summary> /// Computes a hash of a block of binary data. /// </summary> /// <param name="Function">Hash function.</param> /// <param name="Data">Binary data.</param> /// <returns>Hash value.</returns> public static byte[] ComputeHash(HashFunction Function, Stream Data) { switch (Function) { case HashFunction.MD5: return(ComputeMD5Hash(Data)); case HashFunction.SHA1: return(ComputeSHA1Hash(Data)); case HashFunction.SHA256: return(ComputeSHA256Hash(Data)); case HashFunction.SHA384: return(ComputeSHA384Hash(Data)); case HashFunction.SHA512: return(ComputeSHA512Hash(Data)); default: throw new ArgumentException("Unrecognized hash function", nameof(Function)); } }
/// <summary> /// Converts from the Hash enum to the HashAlgorithmName default struct. /// </summary> /// <param name="type">The hash type as a Hash enum</param> /// <returns>The HashAlgorithmName equivalent.</returns> public static HashAlgorithmName ToHashAlgorithmName(this HashFunction type) { switch (type) { case HashFunction.MD5: return(HashAlgorithmName.MD5); case HashFunction.SHA1: return(HashAlgorithmName.SHA1); case HashFunction.SHA256: return(HashAlgorithmName.SHA256); case HashFunction.SHA384: return(HashAlgorithmName.SHA384); case HashFunction.SHA512: return(HashAlgorithmName.SHA512); default: throw new NotSupportedException("No such algorithm name"); } }
public ActionResult Login([Bind(Include = "Email,Password")] AccountVM.LoginViewModel user) { if (ModelState.IsValid) { UserDTO userDTO = new UserDTO(); userDTO = _mapper.Map <AccountVM.LoginViewModel, UserDTO>(user); userDTO.Password = HashFunction.CreateHash(userDTO.Password); bool login = _iapiResponse.Authentication(userDTO); if (login == false) { ModelState.AddModelError("", "Error in login "); return(View()); } else { FormsAuthentication.SetAuthCookie(user.Email, false); return(RedirectToAction("Index", "Home")); } } ModelState.AddModelError("", "Error in login "); return(View()); }
// Hash-based Message Authentication Codes: generates a code for verifying the sender of a message and the like public static byte[] HMAC(byte[] key, byte[] message, HashFunction func, int blockSizeBytes) { if (key.Length > blockSizeBytes) { key = func(key); } else if (key.Length < blockSizeBytes) { byte[] b = new byte[blockSizeBytes]; Array.Copy(key, b, key.Length); key = b; } byte[] o_key_pad = new byte[blockSizeBytes]; // Outer padding byte[] i_key_pad = new byte[blockSizeBytes]; // Inner padding for (int i = 0; i < blockSizeBytes; ++i) { // Combine padding with key o_key_pad[i] = (byte)(key[i] ^ 0x5c); i_key_pad[i] = (byte)(key[i] ^ 0x36); } return(func(Support.Concatenate(o_key_pad, func(Support.Concatenate(message, i_key_pad))))); }
private static void TestEcJPake(string curveName, HashFunction hashFunction) { const string password = "******"; var ecParams = EcInformationStore.GetECCurveData(curveName).GetParameters(); var digest = AuthenticatorFactory.CreateHashPrimitive(hashFunction); var alice = new ECJpakeSession("ObscurCore_P0", password, ecParams, digest, StratCom.EntropySupplier); var bob = new ECJpakeSession("ObscurCore_P1", password, ecParams, digest, StratCom.EntropySupplier); var sw = System.Diagnostics.Stopwatch.StartNew(); // Round 1 var aliceR1 = alice.CreateRound1ToSend(); var bobR1 = bob.CreateRound1ToSend(); alice.ValidateRound1Received(bobR1); bob.ValidateRound1Received(aliceR1); // Round 2 var aliceR2 = alice.CreateRound2ToSend(); var bobR2 = bob.CreateRound2ToSend(); alice.ValidateRound2Received(bobR2); bob.ValidateRound2Received(aliceR2); // Round 3 (key confirmation) byte[] aliceKey, bobKey; var aliceR3 = alice.CreateRound3ToSend(); var bobR3 = bob.CreateRound3ToSend(); alice.ValidateRound3Received(bobR3, out aliceKey); bob.ValidateRound3Received(aliceR3, out bobKey); sw.Stop(); Assert.IsTrue(aliceKey.SequenceEqualShortCircuiting(bobKey), "Keys produced ARE NOT equal! Protocol implementation is broken."); Assert.Pass("{0} ms.\nKey = {1}", sw.ElapsedMilliseconds, aliceKey.ToHexString()); }
private void VerifySignatureButton_Click(object sender, EventArgs e) { if (!IsValidSignature()) { ResultRichTextBox.Text = "Syntax error in Signature"; return; } if (!IsValidPublicKey()) { ResultRichTextBox.Text = "Syntax error in Public Key"; return; } try { DigitalSignature.DigitalSignature signature = GetDigitalSignatureAlgo(); byte[] publicKey = StringToByte(PublicKeyTextBox.Text, 64); byte[] sig = StringToByte(DigitalSignatureTextBox.Text, 64); byte[] hash = new HashFunction(new GOST34112018Policy256bit()).GetHash(Encoding.Default.GetBytes(MessageTextBox.Text)); if (signature.IsSignatureValid(hash, sig, publicKey)) { ResultRichTextBox.Text = "Signature is valid"; } else { ResultRichTextBox.Text = "Signature is invalid"; } } catch (Exception exp) { ResultRichTextBox.Text = "Error occured while signature had been checking. " + exp.Message; } }
public void MultiStateSerializer_Serialise() { // 1. Arrange var options = new CosmosDBStorageOptions { JsonSerializerSettings = new JsonSerializerSettings() }; var hasher = new HashFunction(); var serializer = new MultiStateSerializer(options, hasher); var state = new TestState { Foo = "TestString", Bar = 42, Baz = new Dictionary <string, TestLookupState> { { "One", new TestLookupState { FooBar = true } }, { "Two", new TestLookupState { FooBar = false } }, } }; // 2. Act var result = serializer.Serialize(state); // 3. Assert Assert.NotNull(result); }
/// <summary> /// Creates a new Bloom filter. /// </summary> /// <param name="capacity">The anticipated number of items to be added to the filter. More than this number of items can be added, but the error rate will exceed what is expected.</param> /// <param name="errorRate">The accepable false-positive rate (e.g., 0.01F = 1%)</param> /// <param name="hashFunction">The function to hash the input values. Do not use GetHashCode(). If it is null, and T is string or int a hash function will be provided for you.</param> /// <param name="m">The number of elements in the BitArray.</param> /// <param name="k">The number of hash functions to use.</param> public BloomFilter(int capacity, float errorRate, HashFunction hashFunction, int m, int k) { // validate the params are in range if (capacity < 1) { throw new ArgumentOutOfRangeException(nameof(capacity), capacity, "capacity must be > 0"); } if (errorRate >= 1 || errorRate <= 0) { throw new ArgumentOutOfRangeException(nameof(errorRate), errorRate, $"errorRate must be between 0 and 1, exclusive. Was {errorRate}"); } if (m < 1) // from overflow in bestM calculation { throw new ArgumentOutOfRangeException(nameof(m), $"The provided capacity and errorRate values would result in an array of length > int.MaxValue. Please reduce either of these values. Capacity: {capacity}, Error rate: {errorRate}"); } // set the secondary hash function _getHashSecondary = hashFunction ?? HashString; _hashFunctionCount = k; _bitArray = new BitArray(m); }
private void CalculateSignatureButton_Click(object sender, EventArgs e) { if (!IsValidPrivateKey()) { ResultRichTextBox.Text = "Syntax error in Private Key"; return; } try { DigitalSignature.DigitalSignature signature = GetDigitalSignatureAlgo(); byte[] privateKey = StringToByte(PrivateKeyTextBox.Text, 32); byte[] hash = new HashFunction(new GOST34112018Policy256bit()).GetHash(Encoding.Default.GetBytes(MessageTextBox.Text)); DigitalSignatureTextBox.Text = string.Join("", BitConverter.ToString(signature.CreateSignature(hash, privateKey)).Split('-')); ResultRichTextBox.Text = "Success"; } catch (Exception exp) { ResultRichTextBox.Text = "Error occured while signature had been calculating. " + exp.Message; } }
public void Test100BCSResultsReduced() //takes too long... { int seed = 1; int n = 100000; //100000 int l = 7; int t_min = 29; int t_max = 31; IEnumerable <Tuple <ulong, long> > S = Generator.CreateStreamLong(n, l, seed); HashTableChaining <long> tableMS = new HashTableChaining <long>( HashFunction.MultiplyShift(BitConverter.ToUInt64(Generator.MakeOdd(Generator.GenerateBits(64, seed))), l), 1UL << l ); foreach (Tuple <ulong, long> elem in S) { tableMS.increment(elem.Item1, (NumberLong)elem.Item2); } BigInteger Real_S = Generator.RealCount <long>(tableMS); string path = Directory.GetCurrentDirectory(); DirectoryInfo di = new DirectoryInfo(path); while (di.Name != "XUnit_RAD") { di = di.Parent; } if (!Directory.Exists(Path.Combine(di.FullName, "TestResult"))) { Directory.CreateDirectory(Path.Combine(di.FullName, "TestResult")); } string testfilePath = Path.Combine(di.FullName, "TestResult", "Test100BCSResultsReduced.csv"); if (File.Exists(testfilePath)) { File.Delete(testfilePath); } File.Create(testfilePath).Close(); for (int t = t_min; t <= t_max; t++) { BigInteger[] unsortedResults = new BigInteger[100]; for (int i = 0; i < 100; i++) { BasicCountSketch bcs = new BasicCountSketch(t); foreach (Tuple <ulong, long> elem in S) { bcs.Process(elem); } unsortedResults[i] = bcs.Estimate2ndMoment(); } BigInteger[] manyResults = unsortedResults.OrderBy(val => val).ToArray(); BigInteger meansquare = unsortedResults.Aggregate(BigInteger.Zero, (acc, x) => acc + (BigInteger)Math.Pow((long)x - (long)Real_S, 2)) / 100; BigInteger[] medianResults = new BigInteger[9]; for (int i = 0; i < 9; i++) { medianResults[i] = unsortedResults.Skip(i * 11).Take(11).OrderBy(val => val).ElementAt(5); } medianResults = medianResults.OrderBy(val => val).ToArray(); StringBuilder sb = new StringBuilder(); sb.AppendLine("Real S;" + Real_S + ";Expectation;" + Real_S + "\nVariance;" + Math.Round(2 * Math.Pow((long)Real_S, 2) / (1 << t), 2) + ";Value of t;" + t + "\nValue of m;" + (1 << t) + ";;"); sb.Append("MeanSquareErrors"); BigInteger average = 0; sb.Append(";" + meansquare); average += meansquare; sb.Append(";" + average / 10); sb.AppendLine(); sb.AppendLine("#Estimate;1st run;Average run;Real S"); for (int i = 0; i < 100; i++) { sb.Append(i + 1); average = 0; sb.Append(";" + manyResults[i]); average += manyResults[i]; sb.Append(";" + average / 10); sb.Append(";" + Real_S); sb.AppendLine(); } sb.AppendLine("#Estimate;1st run;Average run;Real S"); for (int i = 0; i < 9; i++) { sb.Append(i + 1); average = 0; sb.Append(";" + medianResults[i]); average += medianResults[i]; sb.Append(";" + average / 10); sb.Append(";" + Real_S); sb.AppendLine(); } sb.AppendLine(";;;"); File.AppendAllText(testfilePath, sb.ToString()); } }
public void Test100BCSResultsParallel() { int seed = 1; int n = 100000; //100000 int l = 17; //2^17 ~ 131000 int t_min = 1; int t_max = 17; //fails due to space on my machine at 29 IEnumerable <Tuple <ulong, long> > S = Generator.CreateStreamLong(n, l, seed); BigInteger[] a_s = new BigInteger[] { new BigInteger(Generator.GenerateBits(89, 1)), new BigInteger(Generator.GenerateBits(89, 2)), new BigInteger(Generator.GenerateBits(89, 3)), new BigInteger(Generator.GenerateBits(89, 4)) }; HashTableChaining <long> table4MMP = new HashTableChaining <long>( HashFunction.CountSketchHashfunctions(HashFunction.kIndependentMultiplyModPrime(a_s), t_max).Item1, 1UL << t_max ); Stopwatch sw = new Stopwatch(); sw.Restart(); foreach (Tuple <ulong, long> elem in S) { table4MMP.increment(elem.Item1, (NumberLong)elem.Item2); } BigInteger Real_S = Generator.RealCount <long>(table4MMP); sw.Stop(); long Real_S_time = sw.ElapsedMilliseconds; string path = Directory.GetCurrentDirectory(); DirectoryInfo di = new DirectoryInfo(path); while (di.Name != "XUnit_RAD") { di = di.Parent; } if (!Directory.Exists(Path.Combine(di.FullName, "TestResult"))) { Directory.CreateDirectory(Path.Combine(di.FullName, "TestResult")); } string testfilePath = Path.Combine(di.FullName, "TestResult", "Test100BCSResultsParallel.csv"); if (File.Exists(testfilePath)) { File.Delete(testfilePath); } File.Create(testfilePath).Close(); for (int t = t_min; t <= t_max; t++) { BigInteger[][] unsortedResults = new BigInteger[10][]; for (int run = 0; run < 10; run++) { Task <BigInteger>[] resultTasks = new Task <BigInteger> [100]; sw.Restart(); for (int i = 0; i < 100; i++) { resultTasks[i] = Task <BigInteger> .Factory.StartNew((object obj) => { IEnumerable <Tuple <ulong, long> > S = (IEnumerable <Tuple <ulong, long> >)obj; BasicCountSketch bcs = new BasicCountSketch(t); foreach (Tuple <ulong, long> elem in S) { bcs.Process(elem); } return(bcs.Estimate2ndMoment()); }, S); } sw.Stop(); Task <BigInteger> .WaitAll(resultTasks); BigInteger[] results = resultTasks.Select(task => task.Result).ToArray(); unsortedResults[run] = results; } long t_iteration_time = sw.ElapsedMilliseconds; BigInteger[][] manyResults = new BigInteger[10][]; BigInteger[][] manyMedianResults = new BigInteger[10][]; BigInteger[] meansquare = new BigInteger[10]; for (int run = 0; run < 10; run++) { manyResults[run] = unsortedResults[run].OrderBy(val => val).ToArray(); meansquare[run] = unsortedResults[run].Aggregate(BigInteger.Zero, (acc, x) => acc + (BigInteger)Math.Pow((long)x - (long)Real_S, 2)) / 100; BigInteger[] medianResults = new BigInteger[9]; for (int i = 0; i < 9; i++) { medianResults[i] = unsortedResults[run].Skip(i * 11).Take(11).OrderBy(val => val).ElementAt(5); } manyMedianResults[run] = medianResults.OrderBy(val => val).ToArray(); } StringBuilder sb = new StringBuilder(); sb.AppendLine("Real S;" + Real_S + ";Expectation;" + Real_S + ";Variance;" + Math.Round(2 * Math.Pow((long)Real_S, 2) / (1 << t), 2) + ";Value of t;" + t + ";Value of m;" + (1 << t) + ";Time taken;" + t_iteration_time + ";"); sb.Append("MeanSquareErrors"); BigInteger average = 0; for (int run = 0; run < 10; run++) { sb.Append(";" + meansquare[run]); average += meansquare[run]; } sb.Append(";" + average / 10); sb.Append(";Real S time;" + Real_S_time); sb.AppendLine(); sb.AppendLine("#Estimate;1st run;2nd run;3rd run;4th run;5th run;6th run;7th run;8th run;9th run;10th run;Average run;Real S"); for (int i = 0; i < 100; i++) { sb.Append(i + 1); average = 0; for (int run = 0; run < 10; run++) { sb.Append(";" + manyResults[run][i]); average += manyResults[run][i]; } sb.Append(";" + average / 10); sb.Append(";" + Real_S); sb.AppendLine(); } sb.AppendLine("#Estimate;1st run;2nd run;3rd run;4th run;5th run;6th run;7th run;8th run;9th run;10th run;Average run;Real S"); for (int i = 0; i < 9; i++) { sb.Append(i + 1); average = 0; for (int run = 0; run < 10; run++) { sb.Append(";" + manyMedianResults[run][i]); average += manyMedianResults[run][i]; } sb.Append(";" + average / 10); sb.Append(";" + Real_S); sb.AppendLine(); } sb.AppendLine(";;;;;;;;;;;;"); File.AppendAllText(testfilePath, sb.ToString()); } }
// HMAC per RFC 2104 private static byte[] hmac(byte[] key, byte[] text, HashFunction hash) { // Working key buffer. (If caller's key is too long, use a hash of it instead.) byte[] K = (key.Length <= HMAC_KEY_LEN) ? key : hash(key); // Build the inner hash buffer byte[] inner = new byte[HMAC_KEY_LEN + text.Length]; Array.Copy(K, 0, inner, 0, K.Length); Array.Copy(text, 0, inner, HMAC_KEY_LEN, text.Length); for (int i = 0; i < HMAC_KEY_LEN; i++) { inner[i] ^= HMAC_IPAD_BYTE; } // Calculate the inner hash value. byte[] innerhash = hash(inner); // Build the outer hash buffer byte[] outer = new byte[HMAC_KEY_LEN + innerhash.Length]; Array.Copy(K, 0, outer, 0, K.Length); Array.Copy(innerhash, 0, outer, HMAC_KEY_LEN, innerhash.Length); for (int i = 0; i < HMAC_KEY_LEN; i++) { outer[i] ^= HMAC_OPAD_BYTE; } // Compute and return the final hash value. return hash(outer); }
private void InitDevice(ParameterSet set) { if (device == null) { if (UseVirtualDevice) { device = new VirtualDevice(set, new BigInteger(1, deviceSecret)); } else { // use a smartcard device = new SmartCardDevice(set.Group, set.Gd, smartCardParam); } Gq = set.Group; HashFunctionOID = SecurityLevelUtils.getHashfunction(set); hash = new HashFunction(HashFunctionOID); Gd = set.Gd; } }
/// <summary> /// Initializes a new instance of the <see cref="FilterEasyCachingRedis"/> class. /// </summary> /// <param name="name"></param> /// <param name="provider">The <see cref="IRedisCachingProvider"/>.</param> /// <param name="redisKey">The redisKey.</param> /// <param name="expectedElements">The expected elements.</param> /// <param name="errorRate">The error rate.</param> /// <param name="hashFunction">The hash function.</param> public FilterEasyCachingRedis(string name, IRedisCachingProvider provider, string redisKey, int expectedElements, double errorRate, HashFunction hashFunction) : base(name, expectedElements, errorRate, hashFunction) { if (string.IsNullOrWhiteSpace(redisKey)) { throw new ArgumentException(nameof(redisKey)); } _provider = provider ?? throw new ArgumentNullException(nameof(provider)); _redisKey = redisKey; }
/// <summary> /// Builds the specified connectionn. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="connectionn">The connectionn.</param> /// <param name="redisKey">The redis key.</param> /// <param name="expectedElements">The expected elements.</param> /// <param name="hashFunction">The hash function.</param> /// <returns></returns> public static Filter <T> Build <T>(IConnectionMultiplexer connectionn, string redisKey, int expectedElements, HashFunction hashFunction) { return(Build <T>(connectionn, redisKey, expectedElements, 0.01, hashFunction)); }
public BloomFilter(HashFunction hashFunctionFlags, int arraySize) { _hashFunctions = GetHashFunctions(hashFunctionFlags); _arraySize = arraySize; _array = new bool[arraySize]; }
//only_structure=false public void hashCode(HashFunction hashfunction, bool only_structure) { SafraTreeNode root = getRootNode(); if (root != null) { root.hashCode(hashfunction, only_structure); } }
/// <summary> /// This method implements the method defined in recommended parameters spec /// </summary> public override GroupElement DeriveElement( byte[] context, byte index, out int counter) { byte[] ggen = new byte[] { (byte)0x67, (byte)0x67, (byte)0x65, (byte)0x6E }; string hashAlg = null; int bitlength = qValue.BitLength; if (bitlength >= 512) { hashAlg = "SHA-512"; } else if (bitlength >= 256) { hashAlg = "SHA-256"; } else if (bitlength >= 160) { hashAlg = "SHA1"; } else { throw new ArgumentException("q is too small"); } HashFunction hash = new HashFunction(hashAlg); // references to "step x" in comments refer to alg from apendix A.2.3 of FIPS 186-3 // int N = this.q.BitLength; // step 2 (usused) BCBigInt e = (pValue - BCBigInt.One) / qValue; // step 3 // prepare U array = context || "ggen" || index || count byte[] contextBytes = (context == null ? new byte[] {} : context); int contextLength = (contextBytes == null ? 0 : contextBytes.Length); byte[] U = new byte[contextLength + ggen.Length + 2]; int arrayIndex = 0; if (contextLength > 0) { Array.Copy(contextBytes, 0, U, arrayIndex, contextLength); arrayIndex += contextLength; } Array.Copy(ggen, 0, U, arrayIndex, ggen.Length); U[U.Length - 2] = index; byte count = 0; // step 4 BCBigInt g = BCBigInt.Zero; while (g < BCBigInt.Two) // step 10 { if (count == 255) { throw new InvalidUProveArtifactException( "can't derive an element; count exceeded"); } count++; // step 5 // complete U array U[U.Length - 1] = count; // step 7 hash.HashWithoutFormatting(U); // BUGBUG: is that ok, will that wrap correctly? BCBigInt W = new BCBigInt(1, hash.Digest); // step 8 g = W.ModPow(e, pValue); // step 9 } counter = count; return new SubgroupGroupElementBCImpl(g, pValue); }
public abstract void hashCode(HashFunction hashfunction);
/// <summary> /// Updates the specified hash function with the group description elements. /// </summary> /// <param name="h">An instanciated hash function.</param> internal override void UpdateHash(HashFunction h) { h.Hash(P); h.Hash(Q); h.Hash(G); }
//todo: check whether need to implement or not /** * Calculate a hash value using HashFunction * @param hashfunction the HashFunction */ //template <class HashFunction> public void hashCode(HashFunction hashfunction) { _L.hashCode(hashfunction); _U.hashCode(hashfunction); }
//=false /** * Calculate a hashvalue using HashFunction for this node. * @param hashfunction the HashFunction functor * @param only_structure Ignore naming of the nodes? */ public void hashCode(HashFunction hashfunction, bool only_structure) { if (!only_structure) { hashfunction.hash(getID()); } getLabeling().hashCode(hashfunction); hashfunction.hash(hasFinalFlag()); if (getChildCount() > 0) { //for (child_iterator cit=children_begin();cit!=children_end();++cit) for (SafraTreeNode cit = children_begin(); cit != children_end(); cit = cit.increment()) { cit.hashCode(hashfunction, only_structure); } } }
internal override void UpdateHash(HashFunction h) { h.Hash(GetEncoded()); }
//only_structure=false /** * Calculate a hash value using HashFunction * @param hashfunction the HashFunction * @param only_structure ignore the nameing of the nodes */ public override void hashCode(HashFunction hashfunction) { hashCode(hashfunction, false); }
public IHmac GetHmacInstance(HashFunction hashFunction) { var sha = _iShaFactory.GetShaInstance(hashFunction); return(new NativeHmac(sha)); }
public void hashCode(HashFunction hashfunction) { bool all_zero = true; for (int i = Count; i > 0; --i) { if (all_zero) { if (bitset[i - 1] != false) { hashfunction.hash(bitset[i - 1]); all_zero = false; } } else { hashfunction.hash(bitset[i - 1]); } } }
public BloomFilter(HashFunction hashFunctionFlags, int estimatedValues, double targetFalsePositiveRate) { _hashFunctions = GetHashFunctions(hashFunctionFlags); _arraySize = GetOptimalM(_hashFunctions.Count(), estimatedValues, targetFalsePositiveRate); _array = new bool[_arraySize]; }
private static byte[] hmac(string key, string text, HashFunction hash) { return hmac(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(text), hash); }
/// <summary> /// Creates a BloomFilter Redis for the specified expected element /// </summary> /// <typeparam name="T"></typeparam> /// <param name="redisBitOperate">The redis bit operate.</param> /// <param name="redisKey">The redis key.</param> /// <param name="expectedElements">The expected elements.</param> /// <param name="hashFunction">The hash function.</param> /// <returns></returns> public static Filter <T> Build <T>(IRedisBitOperate redisBitOperate, string redisKey, int expectedElements, HashFunction hashFunction) { return(Build <T>(redisBitOperate, redisKey, expectedElements, 0.01, hashFunction)); }
/// <summary> /// Creates a BloomFilter Redis for the specified expected element /// </summary> /// <typeparam name="T"></typeparam> /// <param name="configuration">The configuration.</param> /// <param name="redisKey">The redis key.</param> /// <param name="expectedElements">The expected elements.</param> /// <param name="hashFunction">The hash function.</param> /// <returns></returns> public static Filter <T> Build <T>(string configuration, string redisKey, int expectedElements, HashFunction hashFunction) { return(Build <T>(configuration, redisKey, expectedElements, 0.01, hashFunction)); }
/// <summary> /// Creates a BloomFilter Redis for the specified expected element /// </summary> /// <typeparam name="T"></typeparam> /// <param name="configuration">The configuration.</param> /// <param name="redisKey">The redis key.</param> /// <param name="expectedElements">The expected elements.</param> /// <param name="errorRate">The error rate.</param> /// <param name="hashFunction">The hash function.</param> /// <returns></returns> public static Filter <T> Build <T>(string configuration, string redisKey, int expectedElements, double errorRate, HashFunction hashFunction) { return(new FilterRedis <T>(new RedisBitOperate(configuration), redisKey, expectedElements, errorRate, hashFunction)); }
/// <summary> /// Updates the specified hash function with the group element. /// </summary> /// <param name="h">An instanciated hash function.</param> internal override void UpdateHash(HashFunction h) { h.Hash(Point.GetEncoded()); }
/* * INPUT: The initial Msg is the length of the digest size * * MCT(Msg, MaxOutLen, MinOutLen, OutLenIncrement, MaxBlockSize, MinBlockSize) * { * Range = (MaxOutLen – MinOutLen + 1); * OutputLen = MaxOutLen; * BlockRange = (MaxBlockSize – MinBlockSize + 1); * BlockSize = MinBlockSize; * Customization = ""; * * Output[0] = Msg; * for (j = 0; j < 100; j++) * { * for (i = 1; i < 1001; i++) * { * InnerMsg = Left(Output[i-1] || ZeroBits(128), 128); * Output[i] = ParallelHash(InnerMsg, OutputLen, BlockSize, FunctionName, Customization); * Rightmost_Output_bits = Right(Output[i], 16); * OutputLen = MinOutLen + (floor((Rightmost_Output_bits % Range) / OutLenIncrement) * OutLenIncrement); * BlockSize = MinBlockSize + Right(Rightmost_Output_bits, 8) % BlockRange; * Customization = BitsToString(InnerMsg || Rightmost_Output_bits); * } * * OutputJ[j] = Output[1000]; * } * * return OutputJ; * } */ #endregion MonteCarloAlgorithm Pseudocode public MctResult <AlgoArrayResponseWithCustomization> MCTHash(HashFunction function, BitString message, MathDomain outputLength, MathDomain blockSizeDomain, bool hexCustomization, bool isSample) { _hexCustomization = hexCustomization; if (isSample) { NUM_OF_RESPONSES = 3; } var responses = new List <AlgoArrayResponseWithCustomization>(); var i = 0; var j = 0; var min = outputLength.GetDomainMinMax().Minimum; var max = outputLength.GetDomainMinMax().Maximum; var increment = outputLength.GetDomainMinMax().Increment; var minBlockSize = blockSizeDomain.GetDomainMinMax().Minimum; var maxBlockSize = blockSizeDomain.GetDomainMinMax().Maximum; var outputLen = max; var blockSize = minBlockSize; var blockSizeRange = (maxBlockSize - minBlockSize) + 1; var customization = ""; var range = (max - min) + 1; var innerMessage = message.GetDeepCopy(); try { for (i = 0; i < NUM_OF_RESPONSES; i++) { var innerDigest = new BitString(0); var iterationResponse = new AlgoArrayResponseWithCustomization() { }; iterationResponse.Message = innerMessage; iterationResponse.Customization = customization; iterationResponse.BlockSize = blockSize; for (j = 0; j < 1000; j++) { // Might not have 128 bits to pull from so we pad with 0 innerMessage = BitString.ConcatenateBits(innerMessage, BitString.Zeroes(128)) .GetMostSignificantBits(128); function.DigestLength = outputLen; var innerResult = _iParallelHash.HashMessage(function, innerMessage, blockSize, customization); innerDigest = innerResult.Digest.GetDeepCopy(); // Will always have 16 bits to pull from var rightmostBitString = innerDigest.GetLeastSignificantBits(16); var rightmostBits = rightmostBitString.Bits; outputLen = min + (int)System.Math.Floor((double)(rightmostBits.ToInt() % range) / increment) * increment; blockSize = minBlockSize + rightmostBitString.GetLeastSignificantBits(8).Bits.ToInt() % blockSizeRange; customization = GetStringFromBytes(BitString.ConcatenateBits(innerMessage, rightmostBitString).ToBytes()); innerMessage = innerDigest.GetDeepCopy(); } iterationResponse.Digest = innerDigest.GetDeepCopy(); responses.Add(iterationResponse); } } catch (Exception ex) { ThisLogger.Debug($"i count {i}, j count {j}"); ThisLogger.Error(ex); return(new MctResult <AlgoArrayResponseWithCustomization>($"{ex.Message}; {outputLen}")); } return(new MctResult <AlgoArrayResponseWithCustomization>(responses)); }
/// <summary> /// Updates the specified hash function with the group description elements. /// </summary> /// <param name="h">An hash function object.</param> internal override void UpdateHash(HashFunction h) { // desc(Gq) = (p,a,b,g,q,1) byte[] cofactorArray = { 0x01 }; h.Hash(this.p); h.Hash(this.a); h.Hash(this.b); h.Hash(this.G.GetEncoded()); h.Hash(this.Q); h.Hash(cofactorArray); }