-
-
Notifications
You must be signed in to change notification settings - Fork 676
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mitigate risk of hash collision attacks
- Loading branch information
Showing
11 changed files
with
1,136 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
// <auto-generated /> | ||
|
||
#if !NETCOREAPP | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
// Some routines inspired by the Stanford Bit Twiddling Hacks by Sean Eron Anderson: | ||
// http://graphics.stanford.edu/~seander/bithacks.html | ||
|
||
namespace System.Numerics | ||
{ | ||
/// <summary> | ||
/// Utility methods for intrinsic bit-twiddling operations. | ||
/// The methods use hardware intrinsics when available on the underlying platform, | ||
/// otherwise they use optimized software fallbacks. | ||
/// </summary> | ||
internal static class BitOperations | ||
{ | ||
/// <summary> | ||
/// Rotates the specified value left by the specified number of bits. | ||
/// Similar in behavior to the x86 instruction ROL. | ||
/// </summary> | ||
/// <param name="value">The value to rotate.</param> | ||
/// <param name="offset">The number of bits to rotate by. | ||
/// Any value outside the range [0..31] is treated as congruent mod 32.</param> | ||
/// <returns>The rotated value.</returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static uint RotateLeft(uint value, int offset) | ||
=> (value << offset) | (value >> (32 - offset)); | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) All contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
|
||
namespace MessagePack.Formatters | ||
{ | ||
/// <summary> | ||
/// Provides general helpers for creating collections (including dictionaries). | ||
/// </summary> | ||
/// <typeparam name="TCollection">The concrete type of collection to create.</typeparam> | ||
/// <typeparam name="TEqualityComparer">The type of equality comparer that we would hope to pass into the collection's constructor.</typeparam> | ||
internal static class CollectionHelpers<TCollection, TEqualityComparer> | ||
where TCollection : new() | ||
{ | ||
/// <summary> | ||
/// The delegate that will create the collection, if the typical (int count, IEqualityComparer{T} equalityComparer) constructor was found. | ||
/// </summary> | ||
private static Func<int, TEqualityComparer, TCollection> collectionCreator; | ||
|
||
/// <summary> | ||
/// Initializes static members of the <see cref="CollectionHelpers{TCollection, TEqualityComparer}"/> class. | ||
/// </summary> | ||
/// <remarks> | ||
/// Initializes a delegate that is optimized to create a collection of a given size and using the given equality comparer, if possible. | ||
/// </remarks> | ||
static CollectionHelpers() | ||
{ | ||
var ctor = typeof(TCollection).GetTypeInfo().GetConstructor(new Type[] { typeof(int), typeof(TEqualityComparer) }); | ||
if (ctor != null) | ||
{ | ||
ParameterExpression param1 = Expression.Parameter(typeof(int), "count"); | ||
ParameterExpression param2 = Expression.Parameter(typeof(TEqualityComparer), "equalityComparer"); | ||
NewExpression body = Expression.New(ctor, param1, param2); | ||
collectionCreator = Expression.Lambda<Func<int, TEqualityComparer, TCollection>>(body, param1, param2).Compile(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <typeparamref name="TCollection"/> collection. | ||
/// </summary> | ||
/// <param name="count">The number of elements the collection should be prepared to receive.</param> | ||
/// <param name="equalityComparer">The equality comparer to initialize the collection with.</param> | ||
/// <returns>The newly initialized collection.</returns> | ||
/// <remarks> | ||
/// Use of the <paramref name="count"/> and <paramref name="equalityComparer"/> are a best effort. | ||
/// If we can't find a constructor on the collection in the expected shape, we'll just instantiate the collection with its default constructor. | ||
/// </remarks> | ||
internal static TCollection CreateHashCollection(int count, TEqualityComparer equalityComparer) => collectionCreator != null ? collectionCreator.Invoke(count, equalityComparer) : new TCollection(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.