All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.crypto.util.SSHBuilder Maven / Gradle / Ivy

Go to download

The Long Term Stable (LTS) Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains the JCA/JCE provider and low-level API for the BC LTS version 2.73.7 for Java 8 and later.

There is a newer version: 2.73.7
Show newest version
package org.bouncycastle.crypto.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;

import org.bouncycastle.util.Strings;

class SSHBuilder
{
    private final ByteArrayOutputStream bos = new ByteArrayOutputStream();

    public void u32(int value)
    {
        bos.write((value >>> 24) & 0xFF);
        bos.write((value >>> 16) & 0xFF);
        bos.write((value >>> 8) & 0xFF);
        bos.write(value & 0xFF);
    }

    public void writeBigNum(BigInteger n)
    {
        writeBlock(n.toByteArray());
    }

    public void writeBlock(byte[] value)
    {
        u32(value.length);
        try
        {
            bos.write(value);
        }
        catch (IOException e)
        {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }

    public void writeBytes(byte[] value)
    {
        try
        {
            bos.write(value);
        }
        catch (IOException e)
        {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }

    public void writeString(String str)
    {
        writeBlock(Strings.toByteArray(str));
    }

    public byte[] getBytes()
    {
        return bos.toByteArray();
    }

    public byte[] getPaddedBytes()
    {
        return getPaddedBytes(8);
    }

    public byte[] getPaddedBytes(int blockSize)
    {
        int align = bos.size() % blockSize;
        if (0 != align)
        {
            int padCount = blockSize - align;
            for (int i = 1; i <= padCount; ++i)
            {
                bos.write(i);
            }
        }
        return bos.toByteArray();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy