// ĠCopyright Ġ 2 0 2 1 ĠThe ĠCross - Media ĠMeasurement ĠAuthors Ċ // Ċ // ĠLicensed Ġunder Ġthe ĠApache ĠLicense , ĠVersion Ġ 2 . 0 Ġ( the Ġ" License "); Ċ // Ġyou Ġmay Ġnot Ġuse Ġthis Ġfile Ġexcept Ġin Ġcompliance Ġwith Ġthe ĠLicense . Ċ // ĠYou Ġmay Ġobtain Ġa Ġcopy Ġof Ġthe ĠLicense Ġat Ċ // Ċ // ĠĠĠĠĠ Ġhttp :// www . apache . org / licenses / LICENSE - 2 . 0 Ċ // Ċ // ĠUnless Ġrequired Ġby Ġapplicable Ġlaw Ġor Ġagreed Ġto Ġin Ġwriting , Ġsoftware Ċ // Ġdistributed Ġunder Ġthe ĠLicense Ġis Ġdistributed Ġon Ġan Ġ" AS ĠIS " ĠBASIS , Ċ // ĠWITHOUT ĠWARRANTIES ĠOR ĠCONDITIONS ĠOF ĠANY ĠKIND , Ġeither Ġexpress Ġor Ġimplied . Ċ // ĠSee Ġthe ĠLicense Ġfor Ġthe Ġspecific Ġlanguage Ġgoverning Ġpermissions Ġand Ċ // Ġlimitations Ġunder Ġthe ĠLicense . Ċ Ċ package Ġorg . wf an et . measurement . common . crypto Ċ Ċ import Ġcom . google . protobuf . ByteString Ċ import Ġjava . security . PrivateKey Ċ import Ġjava . security . cert . X 5 0 9 Certificate Ċ import Ġkotlin . test . assert Fails With Ċ import Ġkotlin . test . assertFalse Ċ import Ġkotlin . test . assertTrue Ċ import Ġkotlinx . coroutines . run Blocking Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . junit . runners . JUnit 4 Ċ import Ġorg . wf an et . measurement . common . HexString Ċ import Ġorg . wf an et . measurement . common . as Buffered Flow Ċ import Ġorg . wf an et . measurement . common . crypto . testing . FIXED _ CA _ CERT _ PEM _ FILE Ċ import Ġorg . wf an et . measurement . common . crypto . testing . FIXED _ SERVER _ CERT _ PEM _ FILE Ċ import Ġorg . wf an et . measurement . common . crypto . testing . FIXED _ SERVER _ KEY _ FILE Ċ import Ġorg . wf an et . measurement . common . crypto . testing . Signature Subject . Companion . assertThat Ċ import Ġorg . wf an et . measurement . common . flatten Ċ Ċ private Ġval ĠDATA Ġ= ĊĠ ĠByte String . copyFrom Utf 8 (" I Ġam Ġsome Ġdata Ġto Ġsign . ĠI Ġam Ġsome Ġdata Ġto Ġsign . ĠI Ġam Ġsome Ġdata Ġto Ġsign .") Ċ private Ġval ĠSIGN ATURE Ġ= ĊĠ ĠHex String ( ĊĠĠĠ Ġ" 3 0 4 6 0 2 2 1 0 0 F 7 F 7 2 B FFD 5 9 8 D 4 1 7 C 4 E 1 F 5 2 6 5 F 6 E CA 6 1 7 D 1 D 5 5 3 3 F BC 6 C 8 B 9 6 6 2 F 1 C 0 8 A 9 AD 8 E 3 A 0 2 2 1 0 0 B 9 3 E 9 5 6 6 DAC 6 " Ġ+ ĊĠĠĠĠĠ Ġ" F 9 7 0 6 C 8 B 7 A 0 B 6 A 0 0 3 C 1 B 4 2 7 E 8 7 5 7 EF 9 5 F 8 4 E 7 AE CE 2 3 BE 9 A 9 1 4 5 3 " ĊĠ Ġ) Ċ private Ġval ĠALT _ DATA Ġ= ĠByte String . copyFrom Utf 8 (" I Ġam Ġsome Ġalternative Ġdata ") Ċ private Ġval ĠBO G US _ SIGNATURE Ġ= ĊĠ ĠHex String ( ĊĠĠĠ Ġ" 3 0 4 4 0 2 2 0 0 D 3 ACC 8 6 7 DA 6 6 D 3 4 5 8 6 E 9 A 7 B 3 E 7 3 B 3 1 9 E 3 5 F 1 6 9 D 1 3 EC 9 1 2 7 6 1 A 2 AC 2 8 7 EC 4 6 C 1 B 0 2 2 0 1 DE 3 FD 2 2 4 D 6 D 1 DE 0 " Ġ+ ĊĠĠĠĠĠ Ġ" 4 FD 7 C 5 4 3 6 DF 4 1 5 4 5 D 8 5 ACE 4 1 B 2 AA 5 B 8 1 5 B 4 B DB B 8 9 CB 3 3 1 6 6 " ĊĠ Ġ) Ċ Ċ @ RunWith ( JUnit 4 :: class ) Ċ class ĠSignature s Test Ġ{ ĊĠ Ġ@ Test ĊĠ Ġfun Ġ` verify Signature Ġreturns Ġtrue Ġfor Ġvalid Ġsignature `() Ġ{ ĊĠĠĠ ĠassertTrue ( certificate . verify Signature ( DATA , ĠSIGN ATURE . bytes )) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` verify Signature Ġreturns Ġfalse Ġwhen Ġsignature Ġdoes Ġnot Ġmatch Ġdata `() Ġ{ ĊĠĠĠ ĠassertFalse ( certificate . verify Signature ( ALT _ DATA , ĠSIGN ATURE . bytes )) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` verify Signature Ġreturns Ġfalse Ġwhen Ġsignature Ġdoes Ġnot Ġmatch Ġcertificate `() Ġ{ ĊĠĠĠ ĠassertFalse ( alt Certificate . verify Signature ( DATA , ĠSIGN ATURE . bytes )) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` verify And Collect Ġreturns Ġtrue Ġfor Ġvalid Ġsignature `() Ġ{ ĊĠĠĠ Ġval Ġdata Flow Ġ= ĠDATA . as Buffered Flow ( 2 4 ) ĊĊĠĠĠ Ġvar Ġcollected Ġ= ĠByte String . EMPTY ĊĠĠĠ Ġval Ġverified Ġ= Ġrun Blocking Ġ{ ĊĠĠĠĠĠ Ġdata Flow . collect And Verify ( certificate , ĠSIGN ATURE . bytes ) Ġ{ Ġbytes Ġ-> ĊĠĠĠĠĠĠĠ Ġcollected Ġ= Ġcollected . concat ( bytes ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ ĠassertThat ( collected ). isEqualTo ( DATA ) ĊĠĠĠ ĠassertTrue ( verified ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` sign Ġreturns Ġvalid Ġsignature `() Ġ{ ĊĠĠĠ Ġval Ġsignature Ġ= ĠprivateKey . sign ( certificate , ĠDATA ) ĊĊĠĠĠ ĠassertThat ( signature ). isValid For ( certificate , ĠDATA ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` collect And Sign Ġreturns Ġvalid Ġsignature `() Ġ{ ĊĠĠĠ Ġval Ġdata Flow Ġ= ĠDATA . as Buffered Flow ( 2 4 ) ĊĊĠĠĠ Ġvar Ġcollected Ġ= ĠByte String . EMPTY ĊĠĠĠ Ġval Ġsignature Ġ= Ġrun Blocking Ġ{ ĊĠĠĠĠĠ Ġdata Flow . collect And Sign ({ ĠprivateKey . new Signer ( certificate ) Ġ}) Ġ{ Ġbytes Ġ-> ĊĠĠĠĠĠĠĠ Ġcollected Ġ= Ġcollected . concat ( bytes ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ ĠassertThat ( collected ). isEqualTo ( DATA ) ĊĠĠĠ ĠassertThat ( signature ). isValid For ( certificate , ĠDATA ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` verify ing Ġreturns Ġflow Ġwith Ġinput Ġdata Ġwhen Ġsignature Ġis Ġvalid `() Ġ{ ĊĠĠĠ Ġval Ġdata Flow Ġ= ĠDATA . as Buffered Flow ( 2 4 ) ĊĊĠĠĠ Ġval Ġverify ing Flow Ġ= Ġdata Flow . verify ing ( certificate , ĠSIGN ATURE . bytes ) ĊĊĠĠĠ ĠassertThat ( run Blocking Ġ{ Ġverify ing Flow . flatten () Ġ}). isEqualTo ( DATA ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` verify ing Ġthrows Ġfor Ġinvalid Ġflow Ġsignature `() Ġ{ ĊĠĠĠ Ġval Ġdata Flow Ġ= ĠDATA . as Buffered Flow ( 2 4 ) ĊĊĠĠĠ Ġval Ġverify ing Flow Ġ= Ġdata Flow . verify ing ( certificate , ĠBO G US _ SIGNATURE . bytes ) ĊĊĠĠĠ Ġassert Fails With ( Invalid Signature Exception :: class ) Ġ{ Ġrun Blocking Ġ{ Ġverify ing Flow . flatten () Ġ} Ġ} ĊĠ Ġ} ĊĊĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠ Ġval Ġcertificate : ĠX 5 0 9 Certificate Ġ= Ġread Certificate ( FIXED _ SERVER _ CERT _ PEM _ FILE ) ĊĠĠĠ Ġval Ġalt Certificate : ĠX 5 0 9 Certificate Ġ= Ġread Certificate ( FIXED _ CA _ CERT _ PEM _ FILE ) ĊĠĠĠ Ġval ĠprivateKey : ĠPrivate Key Ġ= ĊĠĠĠĠĠ Ġread PrivateKey ( FIXED _ SERVER _ KEY _ FILE , Ġcertificate . publicKey . algorithm ) ĊĠ Ġ} Ċ } Ċ