Ejemplo n.º 1
0
        public void TestBattery()
        {
            // 128-bit key
            var key = Enumerable.Range(0, 16).Select(x => (byte)x).ToArray();

            // SipHash initialized with the key
            var prf = new SipHash(key);

            // Perform the test battery
            var message = new byte[vectors.Length];

            for (int i = 0; i < vectors.Length; i++)
            {
                message[i] = (byte)i;

                // Compute the tag
                var tag = prf.Compute(message, 0, i);
                // Get the target tag
                var targetTag = BitConverter.ToInt64(vectors[i].Select(x => (byte)x).ToArray(), 0);

                if (tag != targetTag)
                {
                    throw new Exception(string.Format("Test vector failed for {0:N}-byte message!", i));
                }
            }
        }
Ejemplo n.º 2
0
        private static void BenchmarkSipHash(int iterations, int length, Action <string> writeLine)
        {
            // Initialize SipHash engine with a random key
            var siphash = new SipHash(GetRandomBytes(16));

            // Generate specified amount of random data
            var data = GetRandomBytes(length);

            // Benchmark
            var stopWatch = Stopwatch.StartNew();

            for (int i = 0; i < iterations; i++)
            {
                siphash.Compute(data, 0, data.Length);
            }
            var elapsed = stopWatch.Elapsed;

            if (writeLine != null)
            {
                writeLine("SipHash benchmark results:");
                writeLine(string.Format(CultureInfo.CurrentUICulture, "- Digested {0} {1} times", BytesToString(data.Length), iterations));
                writeLine(string.Format(CultureInfo.CurrentUICulture, "- Elapsed: {0}", elapsed.ToString(@"hh\:mm\:ss\.fff")));
                writeLine(string.Format(CultureInfo.CurrentUICulture, "- Speed: {0}/s", BytesToString(data.Length / elapsed.TotalSeconds * iterations)));
            }
        }
Ejemplo n.º 3
0
        public IActionResult Post(ICollection <IFormFile> files)
        {
            _logger.LogInformation("CPAPI: Post");

            try
            {
                // Get the siphash key from secret/environment variable
                string siphashKey = Utils.GetSecretOrEnvVar(ConfigurationProperties.HashKey, this.Configuration, this._logger).Trim();
                // validate tika base address
                if (siphashKey == "")
                {
                    _logger.LogWarning("Hash key not valid - cannot generate hash");
                    return(StatusCode((int)System.Net.HttpStatusCode.InternalServerError));
                }
                else
                {
                    _logger.LogDebug("Hash key loaded");
                }
                byte[] siphashKeyBytes = System.Text.Encoding.ASCII.GetBytes(siphashKey);


                // Check that only one file has been uploaded
                if (files.Count != 1)
                {
                    return(BadRequest());
                }
                if (files.FirstOrDefault().Length == 0)
                {
                    return(BadRequest());
                }

                // Get the bytes from the file
                MemoryStream stream = new MemoryStream();
                files.FirstOrDefault().CopyTo(stream);
                byte[] fileBytes = stream.ToArray();

                // Calculate the hash
                System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();
                SipHash hasher     = new SipHash(siphashKeyBytes);
                long    hashResult = hasher.Compute(fileBytes);
                stopwatch.Stop();
                _logger.LogDebug("Hash time (ms): " + stopwatch.ElapsedMilliseconds.ToString());

                // Convert to JSON
                string hasResultAsJson = JsonConvert.SerializeObject(hashResult, Formatting.None);

                // Return final result
                ObjectResult result = new ObjectResult(hasResultAsJson);
                return(result);
            }
            catch (Exception hashEx)
            {
                _logger.LogError("Hash Controller: " + hashEx.Message);
                return(StatusCode((int)System.Net.HttpStatusCode.InternalServerError));
            }
        }
Ejemplo n.º 4
0
        public void TestBattery()
        {
            // 128-bit key
            var key = Enumerable.Range(0, 16).Select(x => (byte)x).ToArray();

            // SipHash initialized with the key
            var prf = new SipHash(key);

            // Perform the test battery
            var message = new byte[vectors.Length];

            for (int i = 0; i < vectors.Length; i++)
            {
                message[i] = (byte)i;

                // Compute the tag
                var tag = prf.Compute(message.AsSpan(0, i));
                // Get the target tag
                var targetTag = BitConverter.ToUInt64(vectors[i].Select(x => (byte)x).ToArray(), 0);

                Assert.True(tag == targetTag, $"Test vector failed for {i:N}-byte message!");
            }
        }