From 566d17f731637df6828bdf32502a0fb123882dbe Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Thu, 9 Oct 2003 11:41:45 +0000 Subject: Initial import --- src/org/noreply/fancydress/type3/Payload.java | 93 +++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/org/noreply/fancydress/type3/Payload.java (limited to 'src/org/noreply/fancydress/type3/Payload.java') diff --git a/src/org/noreply/fancydress/type3/Payload.java b/src/org/noreply/fancydress/type3/Payload.java new file mode 100644 index 0000000..f0cf583 --- /dev/null +++ b/src/org/noreply/fancydress/type3/Payload.java @@ -0,0 +1,93 @@ +package org.noreply.fancydress.type3; + +import java.io.*; +import java.util.zip.*; +import org.noreply.fancydress.crypto.*; +import org.noreply.fancydress.misc.Util; +import org.noreply.fancydress.type3.routing.*; + +public class Payload { + byte[] thisPayload; + + public Payload() { + RoutingSMTP route = new RoutingSMTP("peter@palfrader.org"); + String body = "FROM:Peter\n" + + "SUBJECT:test fancydress\n" + + "\n" + + "blubb\n"; + + /* + // Encode a String into bytes + String inputString = "blahblahblah??"; + byte[] input = inputString.getBytes("UTF-8"); + + // Compress the bytes + byte[] output = new byte[100]; + Deflater compresser = new Deflater(); + compresser.setInput(input); + compresser.finish(); + int compressedDataLength = compresser.deflate(output); + + // Decompress the bytes + Inflater decompresser = new Inflater(); + decompresser.setInput(output, 0, compressedDataLength); + byte[] result = new byte[100]; + int resultLength = decompresser.inflate(result); + decompresser.end(); + */ + try { + PipedOutputStream poMessage = new PipedOutputStream(); + PipedInputStream piMessage = new PipedInputStream( poMessage ); + + Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION); + DeflaterOutputStream compresserStream = new DeflaterOutputStream(poMessage, compresser); + + /* FIXME, do a more clever and robust way of reading/writing */ + + byte[] message = Util.toOctets(body); + compresserStream.write(message, 0, message.length); + compresserStream.close(); + + byte[] compressed = new byte[message.length]; // UGH + int read = piMessage.read(compressed,0,compressed.length); + compressed = Util.slice(compressed, 0, read); + + byte[] padding = CryptoPrimitives.rand( 28*1024 - compressed.length - 22); + + byte[] payload = new byte[28*1024]; + int pos = 0; + + int firstWord = compressed.length; + + payload[pos] = (byte) ( (firstWord >> 8) & 0xff); + pos++; + payload[pos] = (byte) ( firstWord & 0xff); + pos++; + + System.arraycopy(CryptoPrimitives.hash(compressed, padding), 0, payload, pos, CryptoPrimitives.HASH_LEN); + pos += CryptoPrimitives.HASH_LEN; + + System.arraycopy(compressed, 0, payload, pos, compressed.length); + pos += compressed.length; + + System.arraycopy(padding, 0, payload, pos, padding.length); + pos += padding.length; + + if (pos != 28*1024) + throw new Error("did not fill in 28k bytes"); + + thisPayload = payload; + } catch (Exception e) { + throw new Error(e); + }; + + // Return a singleton payload containing: + // Flag 0 | Int(15,LEN(M_C)) | Hash(M_C | PADDING) | M_C | PADDING + + } + + public byte[] asOctets() { + return thisPayload; + } +} + -- cgit v1.2.3