You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SignatureUtils.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C) 2021, Thomas Wolf <thomas.wolf@paranor.ch> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.util;
  11. import java.text.MessageFormat;
  12. import java.util.Locale;
  13. import org.eclipse.jgit.internal.JGitText;
  14. import org.eclipse.jgit.lib.GpgSignatureVerifier.SignatureVerification;
  15. import org.eclipse.jgit.lib.GpgSignatureVerifier.TrustLevel;
  16. import org.eclipse.jgit.lib.PersonIdent;
  17. /**
  18. * Utilities for signature verification.
  19. *
  20. * @since 5.11
  21. */
  22. public final class SignatureUtils {
  23. private SignatureUtils() {
  24. // No instantiation
  25. }
  26. /**
  27. * Writes information about a signature verification to a string.
  28. *
  29. * @param verification
  30. * to show
  31. * @param creator
  32. * of the object verified; used for time zone information
  33. * @param formatter
  34. * to use for dates
  35. * @return a textual representation of the {@link SignatureVerification},
  36. * using LF as line separator
  37. */
  38. public static String toString(SignatureVerification verification,
  39. PersonIdent creator, GitDateFormatter formatter) {
  40. StringBuilder result = new StringBuilder();
  41. // Use the creator's timezone for the signature date
  42. PersonIdent dateId = new PersonIdent(creator,
  43. verification.getCreationDate());
  44. result.append(MessageFormat.format(JGitText.get().verifySignatureMade,
  45. formatter.formatDate(dateId)));
  46. result.append('\n');
  47. result.append(MessageFormat.format(
  48. JGitText.get().verifySignatureKey,
  49. verification.getKeyFingerprint().toUpperCase(Locale.ROOT)));
  50. result.append('\n');
  51. if (!StringUtils.isEmptyOrNull(verification.getSigner())) {
  52. result.append(
  53. MessageFormat.format(JGitText.get().verifySignatureIssuer,
  54. verification.getSigner()));
  55. result.append('\n');
  56. }
  57. String msg;
  58. if (verification.getVerified()) {
  59. if (verification.isExpired()) {
  60. msg = JGitText.get().verifySignatureExpired;
  61. } else {
  62. msg = JGitText.get().verifySignatureGood;
  63. }
  64. } else {
  65. msg = JGitText.get().verifySignatureBad;
  66. }
  67. result.append(MessageFormat.format(msg, verification.getKeyUser()));
  68. if (!TrustLevel.UNKNOWN.equals(verification.getTrustLevel())) {
  69. result.append(' ' + MessageFormat
  70. .format(JGitText.get().verifySignatureTrust, verification
  71. .getTrustLevel().name().toLowerCase(Locale.ROOT)));
  72. }
  73. result.append('\n');
  74. msg = verification.getMessage();
  75. if (!StringUtils.isEmptyOrNull(msg)) {
  76. result.append(msg);
  77. result.append('\n');
  78. }
  79. return result.toString();
  80. }
  81. }