Skip to content

Commit

Permalink
Add serialization helpers for IdentityKeyPair.
Browse files Browse the repository at this point in the history
  • Loading branch information
moxie0 committed Oct 20, 2014
1 parent 931605a commit 2a65257
Show file tree
Hide file tree
Showing 4 changed files with 450 additions and 6 deletions.
5 changes: 5 additions & 0 deletions libaxolotl/protobuf/LocalStorageProtocol.proto
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ message PreKeyRecordStructure {
optional uint32 id = 1;
optional bytes publicKey = 2;
optional bytes privateKey = 3;
}

message IdentityKeyPairStructure {
optional bytes publicKey = 1;
optional bytes privateKey = 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,51 @@
*/
package org.whispersystems.libaxolotl;

import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;

import org.whispersystems.libaxolotl.ecc.Curve;
import org.whispersystems.libaxolotl.ecc.ECPrivateKey;

import static org.whispersystems.libaxolotl.state.StorageProtos.IdentityKeyPairStructure;

/**
* Holder for public and private identity key pair.
*
* @author Moxie Marlinspike
*/
public class IdentityKeyPair {

private final IdentityKey publicKey;
private final IdentityKey publicKey;
private final ECPrivateKey privateKey;

public IdentityKeyPair(IdentityKey publicKey, ECPrivateKey privateKey) {
this.publicKey = publicKey;
this.privateKey = privateKey;
}

public IdentityKeyPair(byte[] serialized) throws InvalidKeyException {
try {
IdentityKeyPairStructure structure = IdentityKeyPairStructure.parseFrom(serialized);
this.publicKey = new IdentityKey(structure.getPublicKey().toByteArray(), 0);
this.privateKey = Curve.decodePrivatePoint(structure.getPrivateKey().toByteArray());
} catch (InvalidProtocolBufferException e) {
throw new InvalidKeyException(e);
}
}

public IdentityKey getPublicKey() {
return publicKey;
}

public ECPrivateKey getPrivateKey() {
return privateKey;
}

public byte[] serialize() {
return IdentityKeyPairStructure.newBuilder()
.setPublicKey(ByteString.copyFrom(publicKey.serialize()))
.setPrivateKey(ByteString.copyFrom(privateKey.serialize()))
.build().toByteArray();
}
}

0 comments on commit 2a65257

Please sign in to comment.