summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordberg.se>2014-06-04 14:51:29 +0200
committerLinus Nordberg <linus@nordberg.se>2014-06-04 14:51:29 +0200
commit14e947db72cf6a3e7df69a033bd64fd69ef40b03 (patch)
tree904540c47f3ecf9897470a6aaacaaea35fce09ad
parent8bc45f79b3bb2819ec0fc165145864b7f06eebb3 (diff)
Make serialise/1 return a binary.
Lists became nested.
-rw-r--r--src/plop.erl81
1 files changed, 36 insertions, 45 deletions
diff --git a/src/plop.erl b/src/plop.erl
index 4161bca..5d29c24 100644
--- a/src/plop.erl
+++ b/src/plop.erl
@@ -208,12 +208,11 @@ spt(LogID, PrivKey, #timestamped_entry{
timestamp = Timestamp,
entry = #plop_entry{type = EntryType, data = EntryData}
}) ->
- BinToSign =
- list_to_binary(serialise(#spt_signed{
- signature_type = certificate_timestamp,
- timestamp = Timestamp,
- entry_type = EntryType,
- signed_entry = EntryData})),
+ BinToSign = serialise(#spt_signed{
+ signature_type = certificate_timestamp,
+ timestamp = Timestamp,
+ entry_type = EntryType,
+ signed_entry = EntryData}),
Signature = #signature{
algorithm = #sig_and_hash_alg{
hash_alg = sha256,
@@ -233,13 +232,12 @@ sth(PrivKey, Tree, #sth_signed{version = Version, timestamp = Timestamp_in}) ->
Timestamp = timestamp(Timestamp_in),
Treesize = ht:size(Tree),
Roothash = ht:tree_hash(Tree),
- BinToSign =
- list_to_binary(serialise(#sth_signed{
- version = Version,
- signature_type = tree_hash,
- timestamp = Timestamp,
- tree_size = Treesize,
- root_hash = Roothash})),
+ BinToSign = serialise(#sth_signed{
+ version = Version,
+ signature_type = tree_hash,
+ timestamp = Timestamp,
+ tree_size = Treesize,
+ root_hash = Roothash}),
Signature = #signature{
algorithm = #sig_and_hash_alg{
hash_alg = sha256,
@@ -361,30 +359,26 @@ timestamp(Timestamp) ->
-spec serialise(plop_entry() | timestamped_entry() | mtl() |
spt() | spt_signed() | sth() | sth_signed() |
- sig_and_hash_alg() | signature()) -> iolist().
+ sig_and_hash_alg() | signature()) -> binary().
serialise(#plop_entry{
type = TypeAtom,
data = Data
}) ->
EntryType = entry_type(TypeAtom),
- [<<EntryType:16,
- Data/binary>>];
+ <<EntryType:16, Data/binary>>;
serialise(#timestamped_entry{
timestamp = Timestamp,
entry = PlopEntry
}) ->
- [<<Timestamp:64>>,
- serialise(PlopEntry)];
+ list_to_binary([<<Timestamp:64>>, serialise(PlopEntry)]);
serialise(#spt{
version = Version,
logid = LogID,
timestamp = Timestamp,
signature = Signature
}) ->
- [<<Version:8,
- LogID/binary,
- Timestamp:64>>,
- serialise(Signature)];
+ list_to_binary([<<Version:8, LogID/binary, Timestamp:64>>,
+ serialise(Signature)]);
serialise(#spt_signed{
version = Version,
signature_type = SigtypeAtom,
@@ -394,20 +388,18 @@ serialise(#spt_signed{
}) ->
Sigtype = signature_type(SigtypeAtom),
Entrytype = entry_type(EntrytypeAtom),
- [<<Version:8,
- Sigtype:8,
- Timestamp:64,
- Entrytype:16,
- Entry/binary>>];
+ <<Version:8,
+ Sigtype:8,
+ Timestamp:64,
+ Entrytype:16,
+ Entry/binary>>;
serialise(#mtl{ % Merkle Tree Leaf.
version = Version,
leaf_type = TypeAtom,
entry = TimestampedEntry
}) ->
LeafType = leaf_type(TypeAtom),
- [<<Version:8,
- LeafType:8>>,
- serialise(TimestampedEntry)];
+ list_to_binary([<<Version:8, LeafType:8>>, serialise(TimestampedEntry)]);
serialise(#sth_signed{ % Signed Tree Head.
version = Version,
signature_type = SigtypeAtom,
@@ -416,18 +408,18 @@ serialise(#sth_signed{ % Signed Tree Head.
root_hash = Roothash
}) ->
Sigtype = signature_type(SigtypeAtom),
- [<<Version:8,
- Sigtype:8,
- Timestamp:64,
- Treesize:64,
- Roothash/binary>>];
+ <<Version:8,
+ Sigtype:8,
+ Timestamp:64,
+ Treesize:64,
+ Roothash/binary>>;
serialise(#sig_and_hash_alg{
hash_alg = HashAlgType,
signature_alg = SignatureAlgType
}) ->
HashAlg = hash_alg_type(HashAlgType),
SignatureAlg = signature_alg_type(SignatureAlgType),
- [<<HashAlg:8, SignatureAlg:8>>];
+ <<HashAlg:8, SignatureAlg:8>>;
serialise(#signature{
algorithm = Algorithm,
signature = Signature % DER encoded.
@@ -436,21 +428,20 @@ serialise(#signature{
%% don't forget that the signature is a vector as specified in
%% section 4.3 and has a length field.
SigLen = size(Signature),
- [serialise(Algorithm),
- <<SigLen:16,
- Signature/binary>>].
+ list_to_binary([serialise(Algorithm),
+ <<SigLen:16, Signature/binary>>]).
%%%%%%%%%%%%%%%%%%%%
%% Internal tests. For more tests see ../test/.
serialise_test_() ->
[?_assertEqual(
<<0:8, 0:8, 0:64, 0:16, "foo">>,
- list_to_binary(serialise(#spt_signed{
- version = 0,
- signature_type = certificate_timestamp,
- timestamp = 0,
- entry_type = x509,
- signed_entry = <<"foo">>})))].
+ serialise(#spt_signed{
+ version = 0,
+ signature_type = certificate_timestamp,
+ timestamp = 0,
+ entry_type = x509,
+ signed_entry = <<"foo">>}))].
add_test() ->
{ok, S} = init([?TESTPRIVKEYFILE, ?TESTPUBKEYFILE]),