package org.bouncycastle.crypto.util;

public abstract class Pack
{
    public static int bigEndianToInt(byte[] bs, int off)
    {
        int n = bs[  off] << 24;
        n |= (bs[++off] & 0xff) << 16;
        n |= (bs[++off] & 0xff) << 8;
        n |= (bs[++off] & 0xff);
        return n;
    }

    public static void intToBigEndian(int n, byte[] bs, int off)
    {
        bs[  off] = (byte)(n >>> 24);
        bs[++off] = (byte)(n >>> 16);
        bs[++off] = (byte)(n >>>  8);
        bs[++off] = (byte)(n       );
    }

    public static long bigEndianToLong(byte[] bs, int off)
    {
        int hi = bigEndianToInt(bs, off);
        int lo = bigEndianToInt(bs, off + 4);
        return ((long)(hi & 0xffffffffL) << 32) | (long)(lo & 0xffffffffL);
    }

    public static void longToBigEndian(long n, byte[] bs, int off)
    {
        intToBigEndian((int)(n >>> 32), bs, off);
        intToBigEndian((int)(n & 0xffffffffL), bs, off + 4);
    }

    public static int littleEndianToInt(byte[] bs, int off)
    {
        int n = bs[  off] & 0xff;
        n |= (bs[++off] & 0xff) << 8;
        n |= (bs[++off] & 0xff) << 16;
        n |= bs[++off] << 24;
        return n;
    }

    public static void littleEndianToInt(byte[] bs, int off, int[] ns)
	{
		for (int i = 0; i < ns.length; ++i)
		{
			ns[i] = littleEndianToInt(bs, off);
			off += 4;
		}
	}

    public static void intToLittleEndian(int n, byte[] bs, int off)
    {
        bs[  off] = (byte)(n       );
        bs[++off] = (byte)(n >>>  8);
        bs[++off] = (byte)(n >>> 16);
        bs[++off] = (byte)(n >>> 24);
    }

	public static void intToLittleEndian(int[] ns, byte[] bs, int off)
	{
		for (int i = 0; i < ns.length; ++i)
		{
			intToLittleEndian(ns[i], bs, off);
			off += 4;
		}
	}

    public static long littleEndianToLong(byte[] bs, int off)
    {
        int lo = littleEndianToInt(bs, off);
        int hi = littleEndianToInt(bs, off + 4);
        return ((long)(hi & 0xffffffffL) << 32) | (long)(lo & 0xffffffffL);
    }

    public static void longToLittleEndian(long n, byte[] bs, int off)
    {
        intToLittleEndian((int)(n & 0xffffffffL), bs, off);
        intToLittleEndian((int)(n >>> 32), bs, off + 4);
    }
}

@@ -319,207 +319,630 @@
 oid 
-intToBigEndian(int n, byte%5B%5D bs, int off)%0A    %7B%0A        bs%5B  off%5D = (byte)(n %3E%3E%3E 24);%0A        bs%5B++off%5D = (byte)(n %3E%3E%3E 16);%0A        bs%5B++off%5D = (byte)(n %3E%3E%3E  8);%0A        bs%5B++off%5D = (byte)(n       );
+bigEndianToInt(byte%5B%5D bs, int off, int%5B%5D ns)%0A    %7B%0A        for (int i = 0; i %3C ns.length; ++i)%0A        %7B%0A            ns%5Bi%5D = bigEndianToInt(bs, off);%0A            off += 4;%0A        %7D%0A    %7D%0A%0A    public static void intToBigEndian(int n, byte%5B%5D bs, int off)%0A    %7B%0A        bs%5B  off%5D = (byte)(n %3E%3E%3E 24);%0A        bs%5B++off%5D = (byte)(n %3E%3E%3E 16);%0A        bs%5B++off%5D = (byte)(n %3E%3E%3E  8);%0A        bs%5B++off%5D = (byte)(n       );%0A    %7D%0A%0A    public static void intToBigEndian(int%5B%5D ns, byte%5B%5D bs, int off)%0A    %7B%0A        for (int i = 0; i %3C ns.length; ++i)%0A        %7B%0A            intToBigEndian(ns%5Bi%5D, bs, off);%0A            off += 4;%0A        %7D
 %0A   

Add variants for packing arrays of ints