GpgSignature.java

  1. /*
  2.  * Copyright (C) 2018, Salesforce. 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.lib;

  11. import static java.nio.charset.StandardCharsets.US_ASCII;

  12. import java.io.Serializable;

  13. import org.eclipse.jgit.annotations.NonNull;

  14. /**
  15.  * A structure for holding GPG signature together with additional related data.
  16.  *
  17.  * @since 5.3
  18.  */
  19. public class GpgSignature implements Serializable {

  20.     private static final long serialVersionUID = 1L;

  21.     private byte[] signature;

  22.     /**
  23.      * Creates a new instance with the specified signature
  24.      *
  25.      * @param signature
  26.      *            the signature
  27.      */
  28.     public GpgSignature(@NonNull byte[] signature) {
  29.         this.signature = signature;
  30.     }

  31.     /**
  32.      * Format for Git storage.
  33.      * <p>
  34.      * This returns the ASCII Armor as per
  35.      * https://tools.ietf.org/html/rfc4880#section-6.2.
  36.      * </p>
  37.      *
  38.      * @return a string of the signature ready to be embedded in a Git object
  39.      */
  40.     public String toExternalString() {
  41.         return new String(signature, US_ASCII);
  42.     }

  43.     /** {@inheritDoc} */
  44.     @Override
  45.     @SuppressWarnings("nls")
  46.     public String toString() {
  47.         final StringBuilder r = new StringBuilder();

  48.         r.append("GpgSignature[");
  49.         r.append(
  50.                 this.signature != null ? "length " + signature.length : "null");
  51.         r.append("]");

  52.         return r.toString();
  53.     }
  54. }