Path: blob/master/src/java.base/share/classes/java/security/Key.java
41152 views
/*1* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.security;2627/**28* The Key interface is the top-level interface for all keys. It29* defines the functionality shared by all key objects. All keys30* have three characteristics:31*32* <UL>33*34* <LI>An Algorithm35*36* <P>This is the key algorithm for that key. The key algorithm is usually37* an encryption or asymmetric operation algorithm (such as DSA or38* RSA), which will work with those algorithms and with related39* algorithms (such as MD5 with RSA, SHA-1 with RSA, Raw DSA, etc.)40* The name of the algorithm of a key is obtained using the41* {@link #getAlgorithm() getAlgorithm} method.42*43* <LI>An Encoded Form44*45* <P>This is an external encoded form for the key used when a standard46* representation of the key is needed outside the Java Virtual Machine,47* as when transmitting the key to some other party. The key48* is encoded according to a standard format (such as49* X.509 {@code SubjectPublicKeyInfo} or PKCS#8), and50* is returned using the {@link #getEncoded() getEncoded} method.51* Note: The syntax of the ASN.1 type {@code SubjectPublicKeyInfo}52* is defined as follows:53*54* <pre>55* SubjectPublicKeyInfo ::= SEQUENCE {56* algorithm AlgorithmIdentifier,57* subjectPublicKey BIT STRING }58*59* AlgorithmIdentifier ::= SEQUENCE {60* algorithm OBJECT IDENTIFIER,61* parameters ANY DEFINED BY algorithm OPTIONAL }62* </pre>63*64* For more information, see65* <a href="http://tools.ietf.org/html/rfc5280">RFC 5280:66* Internet X.509 Public Key Infrastructure Certificate and CRL Profile</a>.67*68* <LI>A Format69*70* <P>This is the name of the format of the encoded key. It is returned71* by the {@link #getFormat() getFormat} method.72*73* </UL>74*75* Keys are generally obtained through key generators, certificates,76* key stores or other classes used to manage keys.77* Keys may also be obtained from key specifications (transparent78* representations of the underlying key material) through the use of a key79* factory (see {@link KeyFactory}).80*81* <p> A Key should use KeyRep as its serialized representation.82* Note that a serialized Key may contain sensitive information83* which should not be exposed in untrusted environments. See the84* <a href="{@docRoot}/../specs/serialization/security.html">85* Security Appendix</a>86* of the Serialization Specification for more information.87*88* @see PublicKey89* @see PrivateKey90* @see KeyPair91* @see KeyPairGenerator92* @see KeyFactory93* @see KeyRep94* @see java.security.spec.KeySpec95* @see Identity96* @see Signer97*98* @author Benjamin Renaud99* @since 1.1100*/101102public interface Key extends java.io.Serializable {103104// Declare serialVersionUID to be compatible with JDK1.1105106/**107* The class fingerprint that is set to indicate108* serialization compatibility with a previous109* version of the class.110*111* @deprecated A {@code serialVersionUID} field in an interface is112* ineffectual. Do not use; no replacement.113*/114@Deprecated115@SuppressWarnings("serial")116@java.io.Serial117static final long serialVersionUID = 6603384152749567654L;118119/**120* Returns the standard algorithm name for this key. For121* example, "DSA" would indicate that this key is a DSA key.122* See the key related sections (KeyFactory, KeyGenerator,123* KeyPairGenerator, and SecretKeyFactory) in the <a href=124* "{@docRoot}/../specs/security/standard-names.html">125* Java Security Standard Algorithm Names Specification</a>126* for information about standard key algorithm names.127*128* @return the name of the algorithm associated with this key.129*/130public String getAlgorithm();131132/**133* Returns the name of the primary encoding format of this key,134* or null if this key does not support encoding.135* The primary encoding format is136* named in terms of the appropriate ASN.1 data format, if an137* ASN.1 specification for this key exists.138* For example, the name of the ASN.1 data format for public139* keys is <I>SubjectPublicKeyInfo</I>, as140* defined by the X.509 standard; in this case, the returned format is141* {@code "X.509"}. Similarly,142* the name of the ASN.1 data format for private keys is143* <I>PrivateKeyInfo</I>,144* as defined by the PKCS #8 standard; in this case, the returned format is145* {@code "PKCS#8"}.146*147* @return the primary encoding format of the key.148*/149public String getFormat();150151/**152* Returns the key in its primary encoding format, or null153* if this key does not support encoding.154*155* @return the encoded key, or null if the key does not support156* encoding.157*/158public byte[] getEncoded();159}160161162