Class SecurityUtils


  • public class SecurityUtils
    extends java.lang.Object
    Static utility method relating to security facilities.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String BOUNCY_CASTLE
      Identifier for the BouncyCastle JCE provider
      private static org.slf4j.Logger LOG  
      private static java.lang.Boolean registerBouncyCastle  
      private static boolean registrationDone  
      private static java.lang.String securityProvider  
      static java.lang.String SPONGY_CASTLE
      Identifier for the BouncyCastle JCE provider
    • Constructor Summary

      Constructors 
      Constructor Description
      SecurityUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static javax.crypto.Cipher getCipher​(java.lang.String transformation)  
      static java.lang.String getFingerprint​(java.security.PublicKey key)
      Computes the fingerprint for a public key, in the standard SSH format, e.g.
      static javax.crypto.KeyAgreement getKeyAgreement​(java.lang.String algorithm)
      Creates a new instance of KeyAgreement with the given algorithm.
      static java.security.KeyFactory getKeyFactory​(java.lang.String algorithm)
      Creates a new instance of KeyFactory with the given algorithm.
      static java.security.KeyPairGenerator getKeyPairGenerator​(java.lang.String algorithm)
      Creates a new instance of KeyPairGenerator with the given algorithm.
      static javax.crypto.Mac getMAC​(java.lang.String algorithm)
      Create a new instance of Mac with the given algorithm.
      static java.security.MessageDigest getMessageDigest​(java.lang.String algorithm)
      Create a new instance of MessageDigest with the given algorithm.
      static java.lang.String getSecurityProvider()
      Get the identifier for the registered security provider.
      static java.security.Signature getSignature​(java.lang.String algorithm)  
      static boolean isBouncyCastleRegistered()
      Attempts registering BouncyCastle as security provider if it has not been previously attempted and returns whether the registration succeeded.
      private static void register()  
      static boolean registerSecurityProvider​(java.lang.String providerClassName)  
      static void setRegisterBouncyCastle​(boolean registerBouncyCastle)
      Configure whether to register the Bouncy Castle Security Provider.
      static void setSecurityProvider​(java.lang.String securityProvider)
      Specifies the JCE security provider that should be used.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOG

        private static final org.slf4j.Logger LOG
      • BOUNCY_CASTLE

        public static final java.lang.String BOUNCY_CASTLE
        Identifier for the BouncyCastle JCE provider
        See Also:
        Constant Field Values
      • SPONGY_CASTLE

        public static final java.lang.String SPONGY_CASTLE
        Identifier for the BouncyCastle JCE provider
        See Also:
        Constant Field Values
      • securityProvider

        private static java.lang.String securityProvider
      • registerBouncyCastle

        private static java.lang.Boolean registerBouncyCastle
      • registrationDone

        private static boolean registrationDone
    • Constructor Detail

      • SecurityUtils

        public SecurityUtils()
    • Method Detail

      • registerSecurityProvider

        public static boolean registerSecurityProvider​(java.lang.String providerClassName)
      • getCipher

        public static javax.crypto.Cipher getCipher​(java.lang.String transformation)
                                             throws java.security.NoSuchAlgorithmException,
                                                    javax.crypto.NoSuchPaddingException,
                                                    java.security.NoSuchProviderException
        Throws:
        java.security.NoSuchAlgorithmException
        javax.crypto.NoSuchPaddingException
        java.security.NoSuchProviderException
      • getFingerprint

        public static java.lang.String getFingerprint​(java.security.PublicKey key)
        Computes the fingerprint for a public key, in the standard SSH format, e.g. "4b:69:6c:72:6f:79:20:77:61:73:20:68:65:72:65:21"
        Parameters:
        key - the public key
        Returns:
        the fingerprint
        See Also:
        specification
      • getKeyAgreement

        public static javax.crypto.KeyAgreement getKeyAgreement​(java.lang.String algorithm)
                                                         throws java.security.NoSuchAlgorithmException,
                                                                java.security.NoSuchProviderException
        Creates a new instance of KeyAgreement with the given algorithm.
        Parameters:
        algorithm - key agreement algorithm
        Returns:
        new instance
        Throws:
        java.security.NoSuchAlgorithmException
        java.security.NoSuchProviderException
      • getKeyFactory

        public static java.security.KeyFactory getKeyFactory​(java.lang.String algorithm)
                                                      throws java.security.NoSuchAlgorithmException,
                                                             java.security.NoSuchProviderException
        Creates a new instance of KeyFactory with the given algorithm.
        Parameters:
        algorithm - key factory algorithm e.g. RSA, DSA
        Returns:
        new instance
        Throws:
        java.security.NoSuchAlgorithmException
        java.security.NoSuchProviderException
      • getKeyPairGenerator

        public static java.security.KeyPairGenerator getKeyPairGenerator​(java.lang.String algorithm)
                                                                  throws java.security.NoSuchAlgorithmException,
                                                                         java.security.NoSuchProviderException
        Creates a new instance of KeyPairGenerator with the given algorithm.
        Parameters:
        algorithm - key pair generator algorithm
        Returns:
        new instance
        Throws:
        java.security.NoSuchAlgorithmException
        java.security.NoSuchProviderException
      • getMAC

        public static javax.crypto.Mac getMAC​(java.lang.String algorithm)
                                       throws java.security.NoSuchAlgorithmException,
                                              java.security.NoSuchProviderException
        Create a new instance of Mac with the given algorithm.
        Parameters:
        algorithm - MAC algorithm
        Returns:
        new instance
        Throws:
        java.security.NoSuchAlgorithmException
        java.security.NoSuchProviderException
      • getMessageDigest

        public static java.security.MessageDigest getMessageDigest​(java.lang.String algorithm)
                                                            throws java.security.NoSuchAlgorithmException,
                                                                   java.security.NoSuchProviderException
        Create a new instance of MessageDigest with the given algorithm.
        Parameters:
        algorithm - MessageDigest algorithm name
        Returns:
        new instance
        Throws:
        java.security.NoSuchAlgorithmException
        java.security.NoSuchProviderException
      • getSecurityProvider

        public static java.lang.String getSecurityProvider()
        Get the identifier for the registered security provider.
        Returns:
        JCE provider identifier
      • getSignature

        public static java.security.Signature getSignature​(java.lang.String algorithm)
                                                    throws java.security.NoSuchAlgorithmException,
                                                           java.security.NoSuchProviderException
        Throws:
        java.security.NoSuchAlgorithmException
        java.security.NoSuchProviderException
      • isBouncyCastleRegistered

        public static boolean isBouncyCastleRegistered()
        Attempts registering BouncyCastle as security provider if it has not been previously attempted and returns whether the registration succeeded.
        Returns:
        whether BC (or SC on Android) registered
      • setRegisterBouncyCastle

        public static void setRegisterBouncyCastle​(boolean registerBouncyCastle)
        Configure whether to register the Bouncy Castle Security Provider. Must be called prior to other methods
        Parameters:
        registerBouncyCastle - Enable or disable Bouncy Castle Provider registration on subsequent method invocation
      • setSecurityProvider

        public static void setSecurityProvider​(java.lang.String securityProvider)
        Specifies the JCE security provider that should be used.
        Parameters:
        securityProvider - identifier for the security provider
      • register

        private static void register()