summary refs log tree commit diff
path: root/crypto/src/crypto/Check.cs
blob: 035304db491f72ed78b35874b5e32c34f300ea6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;

namespace Org.BouncyCastle.Crypto
{
    internal static class Check
    {
        internal static void DataLength(bool condition, string message)
        {
            if (condition)
                ThrowDataLengthException(message);
        }

        internal static void DataLength(byte[] buf, int off, int len, string message)
        {
            if (off > (buf.Length - len))
                ThrowDataLengthException(message);
        }

        internal static void OutputLength(bool condition, string message)
        {
            if (condition)
                ThrowOutputLengthException(message);
        }

        internal static void OutputLength(byte[] buf, int off, int len, string message)
        {
            if (off > (buf.Length - len))
                ThrowOutputLengthException(message);
        }

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        internal static void DataLength<T>(ReadOnlySpan<T> input, int len, string message)
        {
            if (input.Length < len)
                ThrowDataLengthException(message);
        }

        internal static void OutputLength<T>(Span<T> output, int len, string message)
        {
            if (output.Length < len)
                ThrowOutputLengthException(message);
        }
#endif

        internal static void ThrowDataLengthException(string message) => throw new DataLengthException(message);
        internal static void ThrowOutputLengthException(string message) => throw new OutputLengthException(message);
    }
}