From 14e947db72cf6a3e7df69a033bd64fd69ef40b03 Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Wed, 4 Jun 2014 14:51:29 +0200 Subject: Make serialise/1 return a binary. Lists became nested. --- src/plop.erl | 81 +++++++++++++++++++++++++++--------------------------------- 1 file 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), - [<>]; + <>; serialise(#timestamped_entry{ timestamp = Timestamp, entry = PlopEntry }) -> - [<>, - serialise(PlopEntry)]; + list_to_binary([<>, serialise(PlopEntry)]); serialise(#spt{ version = Version, logid = LogID, timestamp = Timestamp, signature = Signature }) -> - [<>, - serialise(Signature)]; + list_to_binary([<>, + 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), - [<>]; + <>; serialise(#mtl{ % Merkle Tree Leaf. version = Version, leaf_type = TypeAtom, entry = TimestampedEntry }) -> LeafType = leaf_type(TypeAtom), - [<>, - serialise(TimestampedEntry)]; + list_to_binary([<>, 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), - [<>]; + <>; serialise(#sig_and_hash_alg{ hash_alg = HashAlgType, signature_alg = SignatureAlgType }) -> HashAlg = hash_alg_type(HashAlgType), SignatureAlg = signature_alg_type(SignatureAlgType), - [<>]; + <>; 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), - <>]. + list_to_binary([serialise(Algorithm), + <>]). %%%%%%%%%%%%%%%%%%%% %% 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]), -- cgit v1.1