< Summary

Information
Class: Renci.SshNet.Security.Org.BouncyCastle.Utilities.BigIntegers
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\util\BigIntegers.cs
Line coverage
25%
Covered lines: 9
Uncovered lines: 26
Coverable lines: 35
Total lines: 95
Line coverage: 25.7%
Branch coverage
18%
Covered branches: 3
Total branches: 16
Branch coverage: 18.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
AsUnsignedByteArray(...)100%10%
AsUnsignedByteArray(...)75%490%
CreateRandomInRange(...)0%120%
GetUnsignedByteLength(...)100%10%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\util\BigIntegers.cs

#LineLine coverage
 1using System;
 2
 3using Renci.SshNet.Security.Org.BouncyCastle.Math;
 4using Renci.SshNet.Security.Org.BouncyCastle.Security;
 5
 6namespace Renci.SshNet.Security.Org.BouncyCastle.Utilities
 7{
 8    /**
 9     * BigInteger utilities.
 10     */
 11    internal abstract class BigIntegers
 12    {
 13        private const int MaxIterations = 1000;
 14
 15        /**
 16        * Return the passed in value as an unsigned byte array.
 17        *
 18        * @param value value to be converted.
 19        * @return a byte array without a leading zero byte if present in the signed encoding.
 20        */
 21        public static byte[] AsUnsignedByteArray(
 22            BigInteger n)
 023        {
 024            return n.ToByteArrayUnsigned();
 025        }
 26
 27        /**
 28         * Return the passed in value as an unsigned byte array of specified length, zero-extended as necessary.
 29         *
 30         * @param length desired length of result array.
 31         * @param n value to be converted.
 32         * @return a byte array of specified length, with leading zeroes as necessary given the size of n.
 33         */
 34        public static byte[] AsUnsignedByteArray(int length, BigInteger n)
 3635        {
 3636            byte[] bytes = n.ToByteArrayUnsigned();
 37
 3638            if (bytes.Length > length)
 039                throw new ArgumentException("standard length exceeded", "n");
 40
 3641            if (bytes.Length == length)
 2842                return bytes;
 43
 844            byte[] tmp = new byte[length];
 845            Array.Copy(bytes, 0, tmp, tmp.Length - bytes.Length, bytes.Length);
 846            return tmp;
 3647        }
 48
 49        /**
 50        * Return a random BigInteger not less than 'min' and not greater than 'max'
 51        *
 52        * @param min the least value that may be generated
 53        * @param max the greatest value that may be generated
 54        * @param random the source of randomness
 55        * @return a random BigInteger value in the range [min,max]
 56        */
 57        public static BigInteger CreateRandomInRange(
 58            BigInteger    min,
 59            BigInteger    max,
 60            // TODO Should have been just Random class
 61            SecureRandom  random)
 062        {
 063            int cmp = min.CompareTo(max);
 064            if (cmp >= 0)
 065            {
 066                if (cmp > 0)
 067                    throw new ArgumentException("'min' may not be greater than 'max'");
 68
 069                return min;
 70            }
 71
 072            if (min.BitLength > max.BitLength / 2)
 073            {
 074                return CreateRandomInRange(BigInteger.Zero, max.Subtract(min), random).Add(min);
 75            }
 76
 077            for (int i = 0; i < MaxIterations; ++i)
 078            {
 079                BigInteger x = new BigInteger(max.BitLength, random);
 080                if (x.CompareTo(min) >= 0 && x.CompareTo(max) <= 0)
 081                {
 082                    return x;
 83                }
 084            }
 85
 86            // fall back to a faster (restricted) method
 087            return new BigInteger(max.Subtract(min).BitLength - 1, random).Add(min);
 088        }
 89
 90        public static int GetUnsignedByteLength(BigInteger n)
 091        {
 092            return (n.BitLength + 7) / 8;
 093        }
 94    }
 95}