< Summary

Class:Azure.Security.KeyVault.Keys.Cryptography.ByteExtensions
Assembly:Azure.Security.KeyVault.Keys
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Keys\src\Cryptography\ByteExtensions.cs
Covered lines:17
Uncovered lines:29
Coverable lines:46
Total lines:115
Line coverage:36.9% (17 of 46)
Covered branches:13
Total branches:36
Branch coverage:36.1% (13 of 36)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
SequenceEqualConstantTime(...)-75%75%
Or(...)-0%100%
Or(...)-0%0%
Xor(...)-100%100%
Xor(...)-70%60%
Take(...)-0%100%
Take(...)-0%0%
Zero(...)-75%50%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Keys\src\Cryptography\ByteExtensions.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5
 6namespace Azure.Security.KeyVault.Keys.Cryptography
 7{
 8    internal static class ByteExtensions
 9    {
 10        internal static bool SequenceEqualConstantTime(this byte[] self, byte[] other)
 11        {
 1012            if (self == null)
 013                throw new ArgumentNullException(nameof(self));
 14
 1015            if (other == null)
 016                throw new ArgumentNullException(nameof(other));
 17
 18            // Constant time comparison of two byte arrays
 1019            uint difference = (uint)self.Length ^ (uint)other.Length;
 20
 18021            for (var i = 0; i < self.Length && i < other.Length; i++)
 22            {
 8023                difference |= (uint)(self[i] ^ other[i]);
 24            }
 25
 1026            return difference == 0;
 27        }
 28
 29        internal static byte[] Or(this byte[] self, byte[] other)
 30        {
 031            return Or(self, other, 0);
 32        }
 33
 34        internal static byte[] Or(this byte[] self, byte[] other, int offset)
 35        {
 036            if (self == null)
 037                throw new ArgumentNullException(nameof(self));
 38
 039            if (other == null)
 040                throw new ArgumentNullException(nameof(other));
 41
 042            if (self.Length > other.Length - offset)
 043                throw new ArgumentException("self and other lengths do not match");
 44
 045            var result = new byte[self.Length];
 46
 047            for (var i = 0; i < self.Length; i++)
 48            {
 049                result[i] = (byte)(self[i] | other[offset + i]);
 50            }
 51
 052            return result;
 53        }
 54
 55        internal static byte[] Xor(this byte[] self, byte[] other, bool inPlace = false)
 56        {
 33657            return Xor(self, other, 0, inPlace);
 58        }
 59
 60        internal static byte[] Xor(this byte[] self, byte[] other, int offset, bool inPlace = false)
 61        {
 33662            if (self == null)
 063                throw new ArgumentNullException(nameof(self));
 64
 33665            if (other == null)
 066                throw new ArgumentNullException(nameof(other));
 67
 33668            if (self.Length > other.Length - offset)
 069                throw new ArgumentException("self and other lengths do not match");
 70
 33671            byte[] result = (inPlace) ? self : new byte[self.Length];
 72
 604873            for (var i = 0; i < self.Length; i++)
 74            {
 268875                result[i] = (byte)(self[i] ^ other[offset + i]);
 76            }
 77
 33678            return result;
 79        }
 80
 81        internal static byte[] Take(this byte[] self, int count)
 82        {
 083            return Take(self, 0, count);
 84        }
 85
 86        internal static byte[] Take(this byte[] self, int offset, int count)
 87        {
 088            if (self == null)
 089                throw new ArgumentNullException(nameof(self));
 90
 091            if (offset < 0)
 092                throw new ArgumentException($"{nameof(offset)} cannot be < 0", nameof(offset));
 93
 094            if (count <= 0)
 095                throw new ArgumentException($"{nameof(count)} cannot be <= 0", nameof(count));
 96
 097            if (offset + count > self.Length)
 098                throw new ArgumentException("offset + count cannot be > self.Length", nameof(count));
 99
 0100            var result = new byte[count];
 101
 0102            Array.Copy(self, offset, result, 0, count);
 103
 0104            return result;
 105        }
 106
 107        internal static void Zero(this byte[] self)
 108        {
 20109            if (self == null)
 0110                throw new ArgumentNullException(nameof(self));
 111
 20112            Array.Clear(self, 0, self.Length);
 20113        }
 114    }
 115}