1 package org.bouncycastle.crypto.util; 2 3 public abstract class Pack 4 { bigEndianToInt(byte[] bs, int off)5 public static int bigEndianToInt(byte[] bs, int off) 6 { 7 int n = bs[ off] << 24; 8 n |= (bs[++off] & 0xff) << 16; 9 n |= (bs[++off] & 0xff) << 8; 10 n |= (bs[++off] & 0xff); 11 return n; 12 } 13 intToBigEndian(int n, byte[] bs, int off)14 public static void intToBigEndian(int n, byte[] bs, int off) 15 { 16 bs[ off] = (byte)(n >>> 24); 17 bs[++off] = (byte)(n >>> 16); 18 bs[++off] = (byte)(n >>> 8); 19 bs[++off] = (byte)(n ); 20 } 21 bigEndianToLong(byte[] bs, int off)22 public static long bigEndianToLong(byte[] bs, int off) 23 { 24 int hi = bigEndianToInt(bs, off); 25 int lo = bigEndianToInt(bs, off + 4); 26 return ((long)(hi & 0xffffffffL) << 32) | (long)(lo & 0xffffffffL); 27 } 28 longToBigEndian(long n, byte[] bs, int off)29 public static void longToBigEndian(long n, byte[] bs, int off) 30 { 31 intToBigEndian((int)(n >>> 32), bs, off); 32 intToBigEndian((int)(n & 0xffffffffL), bs, off + 4); 33 } 34 littleEndianToInt(byte[] bs, int off)35 public static int littleEndianToInt(byte[] bs, int off) 36 { 37 int n = bs[ off]; 38 n |= (bs[++off] & 0xff) << 8; 39 n |= (bs[++off] & 0xff) << 16; 40 n |= (bs[++off] & 0xff) << 24; 41 return n; 42 } 43 intToLittleEndian(int n, byte[] bs, int off)44 public static void intToLittleEndian(int n, byte[] bs, int off) 45 { 46 bs[ off] = (byte)(n ); 47 bs[++off] = (byte)(n >>> 8); 48 bs[++off] = (byte)(n >>> 16); 49 bs[++off] = (byte)(n >>> 24); 50 } 51 littleEndianToLong(byte[] bs, int off)52 public static long littleEndianToLong(byte[] bs, int off) 53 { 54 int lo = littleEndianToInt(bs, off); 55 int hi = littleEndianToInt(bs, off + 4); 56 return ((long)(hi & 0xffffffffL) << 32) | (long)(lo & 0xffffffffL); 57 } 58 longToLittleEndian(long n, byte[] bs, int off)59 public static void longToLittleEndian(long n, byte[] bs, int off) 60 { 61 intToLittleEndian((int)(n & 0xffffffffL), bs, off); 62 intToLittleEndian((int)(n >>> 32), bs, off + 4); 63 } 64 } 65