forked from sshnet/SSH.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCipher.cs
More file actions
100 lines (90 loc) · 3.67 KB
/
Cipher.cs
File metadata and controls
100 lines (90 loc) · 3.67 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#nullable enable
using System;
namespace Renci.SshNet.Security.Cryptography
{
/// <summary>
/// Base class for cipher implementation.
/// </summary>
public abstract class Cipher
{
/// <summary>
/// Gets the minimum data size.
/// </summary>
/// <value>
/// The minimum data size.
/// </value>
public abstract byte MinimumSize { get; }
/// <summary>
/// Gets the size of the authentication tag for ciphers which implement Authenticated Encryption (AE).
/// </summary>
/// <value>
/// When this <see cref="Cipher"/> implements Authenticated Encryption, the size, in bytes,
/// of the authentication tag included in the encrypted message.
/// </value>
public virtual int TagSize { get; }
/// <summary>
/// Sets the sequence number.
/// </summary>
/// <param name="sequenceNumber">The sequence number.</param>
internal virtual void SetSequenceNumber(uint sequenceNumber)
{
}
/// <summary>
/// Encrypts the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>Encrypted data.</returns>
public byte[] Encrypt(byte[] input)
{
return Encrypt(input, 0, input.Length);
}
/// <summary>
/// Encrypts the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="offset">The zero-based offset in <paramref name="input"/> at which to begin encrypting.</param>
/// <param name="length">The number of bytes to encrypt from <paramref name="input"/>.</param>
/// <returns>
/// The encrypted data.
/// </returns>
public abstract byte[] Encrypt(byte[] input, int offset, int length);
/// <summary>
/// Decrypts the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>
/// The decrypted data.
/// </returns>
public virtual byte[] Decrypt(byte[] input)
{
return Decrypt(input, 0, input.Length);
}
/// <summary>
/// Decrypts the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="offset">The zero-based offset in <paramref name="input"/> at which to begin decrypting.</param>
/// <param name="length">The number of bytes to decrypt from <paramref name="input"/>.</param>
/// <returns>
/// The decrypted data.
/// </returns>
public abstract byte[] Decrypt(byte[] input, int offset, int length);
/// <summary>
/// Decrypts the specified input into a given buffer.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="offset">The zero-based offset in <paramref name="input"/> at which to begin decrypting.</param>
/// <param name="length">The number of bytes to decrypt from <paramref name="input"/>.</param>
/// <param name="output">The output buffer to write to.</param>
/// <param name="outputOffset">The zero-based offset in <paramref name="output"/> at which to write decrypted output.</param>
/// <returns>
/// The number of bytes written to <paramref name="output"/>.
/// </returns>
public virtual int Decrypt(byte[] input, int offset, int length, byte[] output, int outputOffset)
{
var plaintext = Decrypt(input, offset, length);
plaintext.AsSpan().CopyTo(output.AsSpan(outputOffset));
return plaintext.Length;
}
}
}