View Javadoc
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  
12  import static java.nio.charset.StandardCharsets.US_ASCII;
13  
14  import java.io.Serializable;
15  
16  import org.eclipse.jgit.annotations.NonNull;
17  
18  /**
19   * A structure for holding GPG signature together with additional related data.
20   *
21   * @since 5.3
22   */
23  public class GpgSignature implements Serializable {
24  
25  	private static final long serialVersionUID = 1L;
26  
27  	private byte[] signature;
28  
29  	/**
30  	 * Creates a new instance with the specified signature
31  	 *
32  	 * @param signature
33  	 *            the signature
34  	 */
35  	public GpgSignature(@NonNull byte[] signature) {
36  		this.signature = signature;
37  	}
38  
39  	/**
40  	 * Format for Git storage.
41  	 * <p>
42  	 * This returns the ASCII Armor as per
43  	 * https://tools.ietf.org/html/rfc4880#section-6.2.
44  	 * </p>
45  	 *
46  	 * @return a string of the signature ready to be embedded in a Git object
47  	 */
48  	public String toExternalString() {
49  		return new String(signature, US_ASCII);
50  	}
51  
52  	/** {@inheritDoc} */
53  	@Override
54  	@SuppressWarnings("nls")
55  	public String toString() {
56  		final StringBuilder r = new StringBuilder();
57  
58  		r.append("GpgSignature[");
59  		r.append(
60  				this.signature != null ? "length " + signature.length : "null");
61  		r.append("]");
62  
63  		return r.toString();
64  	}
65  }