diff options
Diffstat (limited to 'crypto/src/crypto/util/SshBuilder.cs')
-rw-r--r-- | crypto/src/crypto/util/SshBuilder.cs | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/crypto/src/crypto/util/SshBuilder.cs b/crypto/src/crypto/util/SshBuilder.cs new file mode 100644 index 000000000..9f2f35360 --- /dev/null +++ b/crypto/src/crypto/util/SshBuilder.cs @@ -0,0 +1,80 @@ +using System; +using System.IO; + +using Org.BouncyCastle.Math; +using Org.BouncyCastle.Utilities; + +namespace Org.BouncyCastle.Crypto.Utilities +{ + internal class SshBuilder + { + private readonly MemoryStream bos = new MemoryStream(); + + public void U32(uint value) + { + bos.WriteByte(Convert.ToByte(value >> 24 & 0xFF)); + bos.WriteByte(Convert.ToByte(value >> 16 & 0xFF)); + bos.WriteByte(Convert.ToByte(value >> 8 & 0xFF)); + bos.WriteByte(Convert.ToByte(value & 0xFF)); + } + + public void WriteBigNum(BigInteger n) + { + WriteBlock(n.ToByteArray()); + } + + public void WriteBlock(byte[] value) + { + U32((uint)value.Length); + try + { + bos.Write(value, 0, value.Length); + } + catch (IOException e) + { + throw new InvalidOperationException(e.Message, e); + } + } + + public void WriteBytes(byte[] value) + { + try + { + bos.Write(value, 0, value.Length); + } + catch (IOException e) + { + throw new InvalidOperationException(e.Message, e); + } + } + + public void WriteString(string str) + { + WriteBlock(Strings.ToByteArray(str)); + } + + public byte[] GetBytes() + { + return bos.ToArray(); + } + + public byte[] GetPaddedBytes() + { + return GetPaddedBytes(8); + } + + public byte[] GetPaddedBytes(int blockSize) + { + int align = (int)bos.Length % blockSize; + if (0 != align) + { + int padCount = blockSize - align; + for (int i = 1; i <= padCount; ++i) + { + bos.WriteByte(Convert.ToByte(i)); + } + } + return bos.ToArray(); + } + } +} |