Class CryptoPrimitives
- java.lang.Object
-
- org.hyperledger.fabric.sdk.security.CryptoPrimitives
-
- All Implemented Interfaces:
CryptoSuite
public class CryptoPrimitives extends java.lang.Object implements CryptoSuite
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.hyperledger.fabric.sdk.security.CryptoSuite
CryptoSuite.Factory
-
-
Constructor Summary
Constructors Constructor Description CryptoPrimitives()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addCACertificatesToTrustStore(java.io.BufferedInputStream bis)
addCACertificatesToTrustStore adds a CA certs in a stream to the trust store used for signature validationvoid
addCACertificateToTrustStore(java.io.File caCertPem, java.lang.String alias)
addCACertificateToTrustStore adds a CA cert to the set of certificates used for signature validationjava.security.cert.Certificate
bytesToCertificate(byte[] certBytes)
Convert bytes in PEM format to Certificate.java.security.PrivateKey
bytesToPrivateKey(byte[] pemKey)
Return PrivateKey from pem bytes.byte[]
certificateToDER(java.lang.String certificatePEM)
java.lang.String
generateCertificationRequest(java.lang.String subject, java.security.KeyPair pair)
generateCertificationRequestCryptoSuiteFactory
getCryptoSuiteFactory()
Get Crypto Suite Factory for this implementation.java.util.Properties
getProperties()
java.security.KeyStore
getTrustStore()
getTrustStore returns the KeyStore object where we keep trusted certificates.byte[]
hash(byte[] input)
Hash the specified text byte data.void
init()
java.security.KeyPair
keyGen()
Generate a key.void
loadCACertificates(java.util.Collection<java.security.cert.Certificate> certificates)
Set the Certificate Authority certificates to be used when validating a certificate chain of trustvoid
loadCACertificatesAsBytes(java.util.Collection<byte[]> certificatesBytes)
Set the Certificate Authority certificates to be used when validating a certificate chain of trust.byte[]
sign(java.security.PrivateKey key, byte[] data)
Sign the specified byte string.boolean
verify(byte[] pemCertificate, java.lang.String signatureAlgorithm, byte[] signature, byte[] plainText)
Verify the specified signature
-
-
-
Method Detail
-
bytesToCertificate
public java.security.cert.Certificate bytesToCertificate(byte[] certBytes) throws CryptoException
Description copied from interface:CryptoSuite
Convert bytes in PEM format to Certificate.- Specified by:
bytesToCertificate
in interfaceCryptoSuite
- Returns:
- Certificate
- Throws:
CryptoException
-
bytesToPrivateKey
public java.security.PrivateKey bytesToPrivateKey(byte[] pemKey) throws CryptoException
Return PrivateKey from pem bytes.- Parameters:
pemKey
- pem-encoded private key- Returns:
- Throws:
CryptoException
-
verify
public boolean verify(byte[] pemCertificate, java.lang.String signatureAlgorithm, byte[] signature, byte[] plainText) throws CryptoException
Description copied from interface:CryptoSuite
Verify the specified signature- Specified by:
verify
in interfaceCryptoSuite
- Parameters:
pemCertificate
- the certificate of the signer as the contents of the PEM filesignatureAlgorithm
- the algorithm used to create the signature.signature
- the signature to verifyplainText
- the original text that is to be verified- Returns:
true
if the signature is successfully verified; otherwisefalse
.- Throws:
CryptoException
-
getTrustStore
public java.security.KeyStore getTrustStore() throws CryptoException
getTrustStore returns the KeyStore object where we keep trusted certificates. If no trust store has been set, this method will create one.- Returns:
- the trust store as a java.security.KeyStore object
- Throws:
CryptoException
- See Also:
KeyStore
-
addCACertificateToTrustStore
public void addCACertificateToTrustStore(java.io.File caCertPem, java.lang.String alias) throws CryptoException, InvalidArgumentException
addCACertificateToTrustStore adds a CA cert to the set of certificates used for signature validation- Parameters:
caCertPem
- an X.509 certificate in PEM formatalias
- an alias associated with the certificate. Used as shorthand for the certificate during crypto operations- Throws:
CryptoException
InvalidArgumentException
-
addCACertificatesToTrustStore
public void addCACertificatesToTrustStore(java.io.BufferedInputStream bis) throws CryptoException, InvalidArgumentException
addCACertificatesToTrustStore adds a CA certs in a stream to the trust store used for signature validation- Parameters:
bis
- an X.509 certificate stream in PEM format in bytes- Throws:
CryptoException
InvalidArgumentException
-
loadCACertificates
public void loadCACertificates(java.util.Collection<java.security.cert.Certificate> certificates) throws CryptoException
Description copied from interface:CryptoSuite
Set the Certificate Authority certificates to be used when validating a certificate chain of trust- Specified by:
loadCACertificates
in interfaceCryptoSuite
- Parameters:
certificates
- A collection ofCertificate
s- Throws:
CryptoException
-
loadCACertificatesAsBytes
public void loadCACertificatesAsBytes(java.util.Collection<byte[]> certificatesBytes) throws CryptoException
Description copied from interface:CryptoSuite
Set the Certificate Authority certificates to be used when validating a certificate chain of trust.- Specified by:
loadCACertificatesAsBytes
in interfaceCryptoSuite
- Parameters:
certificatesBytes
- a collection of certificates in PEM format- Throws:
CryptoException
-
keyGen
public java.security.KeyPair keyGen() throws CryptoException
Description copied from interface:CryptoSuite
Generate a key.- Specified by:
keyGen
in interfaceCryptoSuite
- Returns:
- the generated key.
- Throws:
CryptoException
-
sign
public byte[] sign(java.security.PrivateKey key, byte[] data) throws CryptoException
Description copied from interface:CryptoSuite
Sign the specified byte string.- Specified by:
sign
in interfaceCryptoSuite
- Parameters:
key
- thePrivateKey
to be used for signingdata
- the byte string to sign- Returns:
- the signed data.
- Throws:
java.lang.ClassCastException
- if the supplied private key is not of typeECPrivateKey
.CryptoException
-
generateCertificationRequest
public java.lang.String generateCertificationRequest(java.lang.String subject, java.security.KeyPair pair) throws InvalidArgumentException
generateCertificationRequest- Specified by:
generateCertificationRequest
in interfaceCryptoSuite
- Parameters:
subject
- The subject to be added to the certificatepair
- Public private key pair- Returns:
- PKCS10CertificationRequest Certificate Signing Request.
- Throws:
org.bouncycastle.operator.OperatorCreationException
InvalidArgumentException
-
hash
public byte[] hash(byte[] input)
Description copied from interface:CryptoSuite
Hash the specified text byte data.- Specified by:
hash
in interfaceCryptoSuite
- Parameters:
input
- the text to hash- Returns:
- the hashed data.
-
getCryptoSuiteFactory
public CryptoSuiteFactory getCryptoSuiteFactory()
Description copied from interface:CryptoSuite
Get Crypto Suite Factory for this implementation.- Specified by:
getCryptoSuiteFactory
in interfaceCryptoSuite
- Returns:
- MUST return the one and only one instance of a factory that produced this crypto suite.
-
init
public void init() throws CryptoException, InvalidArgumentException
-
getProperties
public java.util.Properties getProperties()
- Specified by:
getProperties
in interfaceCryptoSuite
- Returns:
- the
Properties
object containing implementation specific key generation properties
-
certificateToDER
public byte[] certificateToDER(java.lang.String certificatePEM)
-
-