Class CryptoHelper


  • public class CryptoHelper
    extends HelperBase
    Helper for the crypto JavaScript module.
    Since:
    Ptolemy II 11.0
    Version:
    $Id$
    Author:
    Hokeun Kim
    Pt.AcceptedRating:
    Red (cxh)
    Pt.ProposedRating:
    Yellow (eal)
    • Constructor Summary

      Constructors 
      Constructor Description
      CryptoHelper​(java.lang.Object actor, jdk.nashorn.api.scripting.ScriptObjectMirror currentObj)
      Constructor for CryptoHelper.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int getHashLength​(java.lang.String hashAlgorithm)
      Return the hash length for the given hash algorithm.
      int getMacLength​(java.lang.String macAlgorithm)
      Return the MAC (Message Authentication Code) length for the given MAC algorithm.
      java.lang.Object hash​(java.lang.Object input, java.lang.String hashAlgorithm)
      Hash the input with a given hash algorithm and return the hashed result.
      java.lang.Object hmac​(java.lang.Object input, java.lang.Object key, java.lang.String hmacAlgorithm)
      Calculate HMAC (key-Hashed Message Authentication Code) for the given input and key.
      java.security.PrivateKey loadPrivateKey​(java.lang.String filePath)
      Load and return a private key from a RSA private key file in DER format.
      java.security.PublicKey loadPublicKey​(java.lang.String filePath)
      Load and return a public key from a X.509 certificate file in PEM format.
      java.lang.Object privateDecrypt​(java.lang.Object input, java.security.PrivateKey privateKey, java.lang.String cipherAlgorithm)
      Decrypt the input with an asymmetric cipher private key and return the decrypted result.
      java.lang.Object publicEncrypt​(java.lang.Object input, java.security.PublicKey publicKey, java.lang.String cipherAlgorithm)
      Encrypt the input with an asymmetric cipher public key and return the encrypted result.
      java.lang.Object randomBytes​(int size)
      Return an array of randomly generated bytes.
      java.lang.Object signWithPrivateKey​(java.lang.Object input, java.security.PrivateKey privateKey, java.lang.String signAlgorithm)
      Sign the given input data with a private key and return the signature.
      java.lang.Object symmetricDecrypt​(java.lang.Object input, java.lang.Object key, java.lang.String cipherAlgorithm)
      Decrypt the input with a symmetric cipher and return the decrypted result.
      java.lang.Object symmetricEncrypt​(java.lang.Object input, java.lang.Object key, java.lang.String cipherAlgorithm)
      Encrypt the input with a symmetric cipher and return the encrypted result.
      boolean verifySignature​(java.lang.Object data, java.lang.Object signature, java.security.PublicKey publicKey, java.lang.String signAlgorithm)
      Verify the signature for given input data and public key of the signer.
      • Methods inherited from class java.lang.Object

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

      • CryptoHelper

        public CryptoHelper​(java.lang.Object actor,
                            jdk.nashorn.api.scripting.ScriptObjectMirror currentObj)
        Constructor for CryptoHelper.
        Parameters:
        actor - The actor associated with this helper.
        currentObj - The JavaScript object that this is helping.
    • Method Detail

      • getHashLength

        public int getHashLength​(java.lang.String hashAlgorithm)
                          throws IllegalActionException
        Return the hash length for the given hash algorithm.
        Parameters:
        hashAlgorithm - The name of the hash algorithm.
        Returns:
        The hash length for the hash algorithm.
        Throws:
        IllegalActionException - If the specified hash algorithm is not available.
      • getMacLength

        public int getMacLength​(java.lang.String macAlgorithm)
                         throws IllegalActionException
        Return the MAC (Message Authentication Code) length for the given MAC algorithm.
        Parameters:
        macAlgorithm - The name of the MAC algorithm.
        Returns:
        The MAC length for the MAC algorithm
        Throws:
        IllegalActionException
      • hash

        public java.lang.Object hash​(java.lang.Object input,
                                     java.lang.String hashAlgorithm)
                              throws IllegalActionException
        Hash the input with a given hash algorithm and return the hashed result.
        Parameters:
        input - The input in the JavaScript object to be hashed.
        hashAlgorithm - The name of the hash algorithm to be used. (Examples: MD5, SHA-1, SHA-256)
        Returns:
        The hash digested for the given input.
        Throws:
        IllegalActionException - If the specified hash algorithm is not available.
      • hmac

        public java.lang.Object hmac​(java.lang.Object input,
                                     java.lang.Object key,
                                     java.lang.String hmacAlgorithm)
                              throws IllegalActionException
        Calculate HMAC (key-Hashed Message Authentication Code) for the given input and key.
        Parameters:
        input - The input in the JavaScript object to be HMAC hashed.
        key - The key to be used for HMAC calculation.
        hmacAlgorithm - The name of the HMAC algorithm.
        Returns:
        The resulting HMAC.
        Throws:
        IllegalActionException - If the HMAC calculation fails.
      • loadPrivateKey

        public java.security.PrivateKey loadPrivateKey​(java.lang.String filePath)
                                                throws IllegalActionException
        Load and return a private key from a RSA private key file in DER format.
        Parameters:
        filePath - The path for the file that stores a RSA private key in DER format.
        Returns:
        PrivateKey object loaded from the file.
        Throws:
        IllegalActionException - If there is a problem with loading the private key.
      • loadPublicKey

        public java.security.PublicKey loadPublicKey​(java.lang.String filePath)
                                              throws IllegalActionException
        Load and return a public key from a X.509 certificate file in PEM format.
        Parameters:
        filePath - The path for the file that stores a X.509 certificate.
        Returns:
        PublicKey object loaded from the certificate.
        Throws:
        IllegalActionException - If there is a problem with loading the public key.
      • privateDecrypt

        public java.lang.Object privateDecrypt​(java.lang.Object input,
                                               java.security.PrivateKey privateKey,
                                               java.lang.String cipherAlgorithm)
                                        throws IllegalActionException
        Decrypt the input with an asymmetric cipher private key and return the decrypted result.
        Parameters:
        input - The cipher text to be decrypted.
        privateKey - The private key of the destination entity.
        cipherAlgorithm - The name of the asymmetric cipher to be used for decryption.
        Returns:
        The decrypted result in a JavaScript integer array.
        Throws:
        IllegalActionException - If the private key decryption fails.
      • publicEncrypt

        public java.lang.Object publicEncrypt​(java.lang.Object input,
                                              java.security.PublicKey publicKey,
                                              java.lang.String cipherAlgorithm)
                                       throws IllegalActionException
        Encrypt the input with an asymmetric cipher public key and return the encrypted result.
        Parameters:
        input - The clear text message to be encrypted.
        publicKey - The public key of the entity that will decrypt the message.
        cipherAlgorithm - The name of the asymmetric cipher to be used for encryption.
        Returns:
        The encrypted result in a JavaScript integer array.
        Throws:
        IllegalActionException - If the public key encryption fails.
      • randomBytes

        public java.lang.Object randomBytes​(int size)
                                     throws IllegalActionException
        Return an array of randomly generated bytes.
        Parameters:
        size - The number of bytes to be generated.
        Returns:
        A JavaScript integer array that is randomly generated.
        Throws:
        IllegalActionException - If the generated bytes cannot be converted to JavaScript array.
      • signWithPrivateKey

        public java.lang.Object signWithPrivateKey​(java.lang.Object input,
                                                   java.security.PrivateKey privateKey,
                                                   java.lang.String signAlgorithm)
                                            throws IllegalActionException
        Sign the given input data with a private key and return the signature.
        Parameters:
        input - The input data to be signed in a JavaScript object.
        privateKey - The public key of the entity that will decrypt the message.
        signAlgorithm - The name of the algorithm to be used for signing.
        Returns:
        Signature calculated from the input data.
        Throws:
        IllegalActionException
      • symmetricDecrypt

        public java.lang.Object symmetricDecrypt​(java.lang.Object input,
                                                 java.lang.Object key,
                                                 java.lang.String cipherAlgorithm)
                                          throws IllegalActionException
        Decrypt the input with a symmetric cipher and return the decrypted result.
        Parameters:
        input - The cipher text to be decrypted.
        key - The secret key for the cipher.
        cipherAlgorithm - The name of the symmetric cipher algorithm to be used for decryption. (examples: AES-128-CBC, DES-ECB)
        Returns:
        The encrypted result in JavaScript byte array.
        Throws:
        IllegalActionException - If the decryption fails.
      • symmetricEncrypt

        public java.lang.Object symmetricEncrypt​(java.lang.Object input,
                                                 java.lang.Object key,
                                                 java.lang.String cipherAlgorithm)
                                          throws IllegalActionException
        Encrypt the input with a symmetric cipher and return the encrypted result.
        Parameters:
        input - The clear text message to be encrypted.
        key - The secret key for the cipher.
        cipherAlgorithm - The name of the symmetric cipher algorithm to be used for encryption. (examples: AES-128-CBC, DES-ECB)
        Returns:
        The encrypted result in JavaScript byte array.
        Throws:
        IllegalActionException - If the encryption fails.
      • verifySignature

        public boolean verifySignature​(java.lang.Object data,
                                       java.lang.Object signature,
                                       java.security.PublicKey publicKey,
                                       java.lang.String signAlgorithm)
                                throws java.lang.IllegalArgumentException
        Verify the signature for given input data and public key of the signer.
        Parameters:
        data - The input data to be verified.
        signature - The signature to be verified.
        publicKey - The public key to be used for signature verification.
        signAlgorithm - The name of the algorithm to be used for signature verification.
        Returns:
        Whether the signature is valid.
        Throws:
        java.lang.IllegalArgumentException