summary refs log tree commit diff
path: root/crypto/src/openpgp/PgpPad.cs
blob: 48f7f2f44dcb15e696d353487ff904b9a5d94e2c (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
using System;

namespace Org.BouncyCastle.Bcpg.OpenPgp
{
    /// <remarks>Padding functions.</remarks>
    public sealed class PgpPad
    {
        private PgpPad()
        {
        }

        public static byte[] PadSessionData(byte[] sessionInfo)
        {
            byte[] result = new byte[40];

            Array.Copy(sessionInfo, 0, result, 0, sessionInfo.Length);

            byte padValue = (byte)(result.Length - sessionInfo.Length);

            for (int i = sessionInfo.Length; i != result.Length; i++)
            {
                result[i] = padValue;
            }

            return result;
        }

        public static byte[] UnpadSessionData(byte[] encoded)
        {
            byte padValue = encoded[encoded.Length - 1];

            for (int i = encoded.Length - padValue; i != encoded.Length; i++)
            {
                if (encoded[i] != padValue)
                    throw new PgpException("bad padding found in session data");
            }

            byte[] taggedKey = new byte[encoded.Length - padValue];

            Array.Copy(encoded, 0, taggedKey, 0, taggedKey.Length);

            return taggedKey;
        }
    }
}