Files
briar/components/net/sf/briar/protocol/AuthorFactoryImpl.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

43 lines
1.3 KiB
Java

package net.sf.briar.protocol;
import java.io.ByteArrayOutputStream;
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.serial.Writer;
import net.sf.briar.api.serial.WriterFactory;
import com.google.inject.Inject;
class AuthorFactoryImpl implements AuthorFactory {
private final CryptoComponent crypto;
private final WriterFactory writerFactory;
@Inject
AuthorFactoryImpl(CryptoComponent crypto, WriterFactory writerFactory) {
this.crypto = crypto;
this.writerFactory = writerFactory;
}
public Author createAuthor(String name, byte[] publicKey)
throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
new AuthorImpl(null, name, publicKey).writeTo(w);
MessageDigest messageDigest = crypto.getMessageDigest();
messageDigest.reset();
messageDigest.update(out.toByteArray());
AuthorId id = new AuthorId(messageDigest.digest());
return new AuthorImpl(id, name, publicKey);
}
public Author createAuthor(AuthorId id, String name, byte[] publicKey) {
return new AuthorImpl(id, name, publicKey);
}
}