Files
briar/components/net/sf/briar/protocol/AuthorReader.java
akwizgran 3f61d0c3df Changed the message format to store the author and group inline - this
doesn't take a huge amount of space and allows every message to be
self-certifying.
2011-07-25 21:14:16 +01:00

39 lines
1.2 KiB
Java

package net.sf.briar.protocol;
import java.io.IOException;
import java.security.MessageDigest;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.protocol.Author;
import net.sf.briar.api.protocol.AuthorFactory;
import net.sf.briar.api.protocol.AuthorId;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.ObjectReader;
import net.sf.briar.api.serial.Reader;
class AuthorReader implements ObjectReader<Author> {
private final MessageDigest messageDigest;
private final AuthorFactory authorFactory;
AuthorReader(CryptoComponent crypto, AuthorFactory authorFactory) {
messageDigest = crypto.getMessageDigest();
this.authorFactory = authorFactory;
}
public Author readObject(Reader r) throws IOException {
// Initialise the consumer
DigestingConsumer digesting = new DigestingConsumer(messageDigest);
messageDigest.reset();
// Read and digest the data
r.addConsumer(digesting);
r.readUserDefinedTag(Tags.AUTHOR);
String name = r.readString();
byte[] publicKey = r.readBytes();
r.removeConsumer(digesting);
// Build and return the author
AuthorId id = new AuthorId(messageDigest.digest());
return authorFactory.createAuthor(id, name, publicKey);
}
}