org.bouncycastle.jcajce.util.AnnotatedPrivateKey Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcprov-jdk15to18 Show documentation
Show all versions of bcprov-jdk15to18 Show documentation
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5 to JDK 1.8.
package org.bouncycastle.jcajce.util;
import java.security.PrivateKey;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Wrapper for a private key that carries annotations that can be used
* for tracking or debugging.
*/
public class AnnotatedPrivateKey
implements PrivateKey
{
public static final String LABEL = "label";
private final PrivateKey key;
private final Map annotations;
AnnotatedPrivateKey(PrivateKey key, String label)
{
this.key = key;
this.annotations = Collections.singletonMap(LABEL, (Object)label);
}
AnnotatedPrivateKey(PrivateKey key, Map annotations)
{
this.key = key;
this.annotations = annotations;
}
public PrivateKey getKey()
{
return key;
}
public Map getAnnotations()
{
return annotations;
}
public String getAlgorithm()
{
return key.getAlgorithm();
}
public Object getAnnotation(String key)
{
return annotations.get(key);
}
/**
* Return a new annotated key with an additional annotation added to it.
*
* @param name the name of the annotation to add.
* @param annotation the object providing the annotation details.
* @return a new annotated key with the extra annotation.
*/
public AnnotatedPrivateKey addAnnotation(String name, Object annotation)
{
Map newAnnotations = new HashMap(annotations);
newAnnotations.put(name, annotation);
return new AnnotatedPrivateKey(this.key, Collections.unmodifiableMap(newAnnotations));
}
/**
* Return a new annotated key with the named annotation removed.
*
* @param name the name of the annotation to remove.
* @return a new annotated key with the named annotation removed.
*/
public AnnotatedPrivateKey removeAnnotation(String name)
{
Map newAnnotations = new HashMap(annotations);
newAnnotations.remove(name);
return new AnnotatedPrivateKey(this.key, Collections.unmodifiableMap(newAnnotations));
}
public String getFormat()
{
return key.getFormat();
}
public byte[] getEncoded()
{
return key.getEncoded();
}
public int hashCode()
{
return this.key.hashCode();
}
public boolean equals(Object o)
{
if (o instanceof AnnotatedPrivateKey)
{
return this.key.equals(((AnnotatedPrivateKey)o).key);
}
return this.key.equals(o);
}
public String toString()
{
if (annotations.containsKey(LABEL))
{
return annotations.get(LABEL).toString();
}
return key.toString();
}
}