コード例 #1
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var seed = _cache.GetSeed();

            var privateAddress = typeof(IPAddress)
                                 .GetProperty("PrivateAddress", BindingFlags.Instance | BindingFlags.NonPublic)?
                                 .GetValue(context.HttpContext.Connection.RemoteIpAddress);

            if (privateAddress != default)
            {
                unsafe
                {
                    var address      = (uint)privateAddress;
                    var addressBytes = stackalloc byte[4];
                    addressBytes[0] = (byte)address;
                    addressBytes[1] = (byte)(address >> 8);
                    addressBytes[2] = (byte)(address >> 16);
                    addressBytes[3] = (byte)(address >> 24);

                    var hash = BitConverter.GetBytes(WyHash64.ComputeHash64(new ReadOnlySpan <byte>(addressBytes, 4),
                                                                            seed));
                    context.ActionArguments["addressHash"] = hash;
                }
            }
            else
            {
                privateAddress = typeof(IPAddress).GetField("_numbers", BindingFlags.Instance | BindingFlags.NonPublic)?
                                 .GetValue(context.HttpContext.Connection.RemoteIpAddress);

                if (privateAddress != default)
                {
                    unsafe
                    {
                        var address      = (ushort[])privateAddress;
                        var addressBytes = stackalloc byte[16];
                        var j            = 0;
                        for (var i = 0; i < 8; i++)
                        {
                            addressBytes[j++] = (byte)((address[i] >> 8) & 0xFF);
                            addressBytes[j++] = (byte)(address[i] & 0xFF);
                        }

                        var hash = BitConverter.GetBytes(
                            WyHash64.ComputeHash64(new ReadOnlySpan <byte>(addressBytes, 16), seed));
                        context.ActionArguments["addressHash"] = hash;
                    }
                }
            }

            base.OnActionExecuting(context);
        }
コード例 #2
0
        public static string StrongETag <TRegion>(this byte[] data, ICacheRegion <TRegion> cache)
        {
            var value = WyHash64.ComputeHash64(data, cache.GetSeed());

            return($"\"{value}\"");
        }
コード例 #3
0
        public static string WeakETag <TRegion>(this string key, bool prefix, ICacheRegion <TRegion> cache)
        {
            var value = WyHash64.ComputeHash64(Encoding.UTF8.GetBytes(key), cache.GetSeed());

            return(prefix ? $"W/\"{value}\"" : $"\"{value}\"");
        }