List of usage examples for org.apache.commons.io EndianUtils readSwappedInteger
public static int readSwappedInteger(byte[] data, int offset)
From source file:com.github.bmadecoder.Authenticator.java
private static int selectInt(byte input[]) { int i = selectPos(input); byte[] subarray = ArrayUtils.subarray(input, i, i + 4); subarray[0] = (byte) (subarray[0] & 0x7f); int readSwappedInteger = EndianUtils.readSwappedInteger(subarray, 0); int result = EndianUtils.swapInteger(readSwappedInteger); return result; }
From source file:ch.threema.apitool.CryptTool.java
/** * Decrypt a message./*from w ww.j a v a 2 s . c o m*/ * * @param box the box to be decrypted * @param recipientPrivateKey the private key of the receiving ID * @param senderPublicKey the public key of the sending ID * @param nonce the nonce that was used for the encryption * @return decrypted message (text or delivery receipt) */ public static ThreemaMessage decryptMessage(byte[] box, byte[] recipientPrivateKey, byte[] senderPublicKey, byte[] nonce) throws MessageParseException { byte[] data = decrypt(box, recipientPrivateKey, senderPublicKey, nonce); if (data == null) throw new DecryptionFailedException(); /* remove padding */ int padbytes = data[data.length - 1] & 0xFF; int realDataLength = data.length - padbytes; if (realDataLength < 1) throw new BadMessageException(); /* Bad message padding */ /* first byte of data is type */ int type = data[0] & 0xFF; switch (type) { case TextMessage.TYPE_CODE: /* Text message */ if (realDataLength < 2) throw new BadMessageException(); try { return new TextMessage(new String(data, 1, realDataLength - 1, "UTF-8")); } catch (UnsupportedEncodingException e) { /* should never happen, UTF-8 is always supported */ throw new RuntimeException(e); } case DeliveryReceipt.TYPE_CODE: /* Delivery receipt */ if (realDataLength < MessageId.MESSAGE_ID_LEN + 2 || ((realDataLength - 2) % MessageId.MESSAGE_ID_LEN) != 0) throw new BadMessageException(); DeliveryReceipt.Type receiptType = DeliveryReceipt.Type.get(data[1] & 0xFF); if (receiptType == null) throw new BadMessageException(); List<MessageId> messageIds = new LinkedList<MessageId>(); int numMsgIds = ((realDataLength - 2) / MessageId.MESSAGE_ID_LEN); for (int i = 0; i < numMsgIds; i++) { messageIds.add(new MessageId(data, 2 + i * MessageId.MESSAGE_ID_LEN)); } return new DeliveryReceipt(receiptType, messageIds); case ImageMessage.TYPE_CODE: if (realDataLength != (1 + ThreemaMessage.BLOB_ID_LEN + 4 + NaCl.NONCEBYTES)) { System.out.println(String.valueOf(realDataLength)); System.out.println(String.valueOf(1 + ThreemaMessage.BLOB_ID_LEN + 4 + NaCl.NONCEBYTES)); throw new BadMessageException(); } byte[] blobId = new byte[ThreemaMessage.BLOB_ID_LEN]; System.arraycopy(data, 1, blobId, 0, ThreemaMessage.BLOB_ID_LEN); int size = EndianUtils.readSwappedInteger(data, 1 + ThreemaMessage.BLOB_ID_LEN); byte[] fileNonce = new byte[NaCl.NONCEBYTES]; System.arraycopy(data, 1 + 4 + ThreemaMessage.BLOB_ID_LEN, fileNonce, 0, nonce.length); return new ImageMessage(blobId, size, fileNonce); case FileMessage.TYPE_CODE: try { return FileMessage.fromString(new String(data, 1, realDataLength - 1, "UTF-8")); } catch (UnsupportedEncodingException e) { throw new BadMessageException(); } default: throw new UnsupportedMessageTypeException(); } }
From source file:io.github.dsheirer.source.tuner.hackrf.HackRFTunerController.java
public int read(Request request, int value, int index, int length) throws UsbException { if (!(length == 1 || length == 2 || length == 4)) { throw new IllegalArgumentException( "invalid length [" + length + "] must be: byte=1, short=2, int=4 to read a primitive"); }//from www . j a v a 2 s. c o m ByteBuffer buffer = readArray(request, value, index, length); byte[] data = new byte[buffer.capacity()]; buffer.get(data); switch (data.length) { case 1: return data[0]; case 2: return EndianUtils.readSwappedShort(data, 0); case 4: return EndianUtils.readSwappedInteger(data, 0); default: throw new UsbException( "read() primitive returned an " + "unrecognized byte array " + Arrays.toString(data)); } }