Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

38
LINES

< > BotCompany Repo | #1001567 // getAuthorOfSignedText [author = computer ID]

JavaX fragment (include)

1  
sbool getAuthorOfSignedText_debug;
2  
3  
sS getAuthorOfSignedText(S signed) {
4  
  ret pairB(getAuthorOfSignedText2(signed));
5  
}
6  
7  
// returns pair(purported author, verified author)
8  
static PairS getAuthorOfSignedText2(S signed) {
9  
  try {
10  
    L<S> lines = toLines(signed);
11  
    S authorLine = nextToLast(lines);
12  
    S sigLine = last(lines);
13  
    if (getAuthorOfSignedText_debug) print("authorLine: " + authorLine);
14  
    S text = fromLines(dropLast(lines));
15  
    //print("Verifying text: " + l(text) + " - " + quote(text));
16  
    if (!startsWith(authorLine, "-")) null;
17  
    S author = dropPrefix("-", authorLine);
18  
    author = beforeComma(author); // drop date etc.
19  
    if (!isComputerID(author)) fail("Not a computer ID: " + author);
20  
    //print("Possible author: " + author);
21  
    S publicKey = getPublicKeyOfComputer(author);
22  
    if (publicKey == null)
23  
      fail("Computer " + author + " has to publish a public key.");
24  
    if (!startsWith(sigLine, "sig: ")) null;
25  
    S sig = dropPrefix("sig: ", sigLine);
26  
    if (!PKI.verifySignature(PKI.publicKeyFromString(publicKey), PKI.signatureFromString(sig), toUtf8(text))) {
27  
      print("Signature doesn't verify (key: ..." + publicKey + ")");
28  
      print("Sig: " + sig);
29  
      print("Text:");
30  
      printIndent(">> ", text);
31  
      ret pair(author, null);
32  
    }
33  
    ret pair(author, author);
34  
  } catch (RuntimeException e) {
35  
    printShortException(e);
36  
    null;
37  
  }
38  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001567
Snippet name: getAuthorOfSignedText [author = computer ID]
Eternal ID of this version: #1001567/19
Text MD5: f778363668abf658ab9e0d791e0078b6
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-06-09 15:25:26
Source code size: 1436 bytes / 38 lines
Pitched / IR pitched: No / No
Views / Downloads: 721 / 1013
Version history: 18 change(s)
Referenced in: [show references]