Solution.vala 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. namespace Riddle {
  2. public sealed class Solution {
  3. public uint8[] data { get; private set; }
  4. private uint8[] secret { get; private set; }
  5. internal uint8[] reply_key { get; set; }
  6. public Solution(uint8[] secret, uint8[] data) {
  7. this.data = data;
  8. this.secret = secret;
  9. }
  10. public uint8[] encrypt_connection_details(InetSocketAddress address) {
  11. print(@"Addy: $(address.address.to_string())\n");
  12. var serialised = serialise_address(address);
  13. return Sodium.Asymmetric.Sealing.seal(serialised, reply_key);
  14. }
  15. public static uint8[]? verify_solved_response(Message response, uint8[] author_signing_key) {
  16. return Sodium.Asymmetric.Signing.verify(Base64.decode(response.items[0]), author_signing_key);
  17. }
  18. public static InetSocketAddress? decrypt_connection_details(uint8[] verified_response, uint8[] reply_public_key, uint8[] reply_secret_key) {
  19. var data = Sodium.Asymmetric.Sealing.unseal(verified_response, reply_public_key, reply_secret_key);
  20. if(data == null) {
  21. return null;
  22. }
  23. var parts = ((string)data).split(" ", 2);
  24. return new InetSocketAddress.from_string(parts[0], uint.parse(parts[1]));
  25. }
  26. private uint8[] serialise_address(InetSocketAddress address) {
  27. var s = @"$(address.address) $(address.port)";
  28. return s.data;
  29. }
  30. public SolutionEnvelope seal(string riddle_id, uint8[] riddle_encryption_key) {
  31. var encrypted_data = Sodium.Asymmetric.Sealing.seal(data, riddle_encryption_key);
  32. var encrypted_reply_key = Sodium.Asymmetric.Sealing.seal(reply_key, riddle_encryption_key);
  33. return new SolutionEnvelope() {
  34. identifier = riddle_id,
  35. signed_data = Sodium.Asymmetric.Signing.sign(encrypted_data, secret),
  36. signed_reply_key = Sodium.Asymmetric.Signing.sign(encrypted_reply_key, secret)
  37. };
  38. }
  39. }
  40. }