All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.autonomy.aci.client.util.EncryptionCodecUtils Maven / Gradle / Ivy

/*
 * Copyright 2006-2018 Open Text.
 *
 * Licensed under the MIT License (the "License"); you may not use this file
 * except in compliance with the License.
 *
 * The only warranties for products and services of Open Text and its affiliates
 * and licensors ("Open Text") are as may be set forth in the express warranty
 * statements accompanying such products and services. Nothing herein should be
 * construed as constituting an additional warranty. Open Text shall not be
 * liable for technical or editorial errors or omissions contained herein. The
 * information contained herein is subject to change without notice.
 */

package com.autonomy.aci.client.util;

import com.autonomy.aci.client.transport.EncryptionCodec;
import com.autonomy.aci.client.transport.EncryptionCodecException;

import java.io.UnsupportedEncodingException;

/**
 * This is a utility class that encrypts and decrypts String objects, it main function is to hide the required
 * conversions between strings and byte arrays.
 */
public class EncryptionCodecUtils {

    // Thread safe singleton instance of our class.
    private static final EncryptionCodecUtils INSTANCE = new EncryptionCodecUtils();

    /**
     * Returns the thread safe singleton instance of this class.
     * @return The thread safe singleton instance of this class
     */
    public static EncryptionCodecUtils getInstance() {
        return INSTANCE;
    }

    /**
     * Encrypt the given string with the supplied codec and use the supplied charset name for any string/byte
     * conversions.
     * @param codec       The EncryptionCodec to use
     * @param string      The input to encrypt
     * @param charsetName The name of charset to use for string and byte array conversion
     * @return The encrypted input
     * @throws EncryptionCodecException if something went wrong during either the conversion of encrypting
     */
    public String encrypt(final EncryptionCodec codec, final String string, final String charsetName) throws EncryptionCodecException {
        final byte[] bytes = toBytes(string, charsetName);
        final byte[] encrypted = codec.encrypt(bytes);
        return toString(encrypted, charsetName);
    }

    /**
     * Decrypt the given string with the supplied codec and use the supplied charset name for any string/byte conversions.
     * @param codec       The EncryptionCodec to use
     * @param string      The input to decrypt
     * @param charsetName The name of charset to use for string and byte array conversion
     * @return The decrypted input
     * @throws EncryptionCodecException if something went wrong during either the conversion of decrypting.
     */
    public String decrypt(final EncryptionCodec codec, final String string, final String charsetName) throws EncryptionCodecException {
        final byte[] bytes = toBytes(string, charsetName);
        final byte[] decrypted = codec.decrypt(bytes);
        return toString(decrypted, charsetName);
    }

    /**
     * Constructs a new String by decoding the specified array of bytes using the specified charset. The length
     * of the new String is a function of the charset, and hence may not be equal to the length of the byte
     * array.
     * 

* The behavior of this method when the given bytes are not valid in the given charset is unspecified. The {@link * java.nio.charset.CharsetDecoder} class should be used when more control over the decoding process is required. * @param bytes the bytes to be decoded into characters * @param charsetName the name of a supported {@link java.nio.charset.Charset charset} * @return A string representation of the bytes argument * @throws EncryptionCodecException If the named charset is not supported * @throws NullPointerException If either bytes or charsetName is null */ public String toString(final byte[] bytes, final String charsetName) throws EncryptionCodecException { try { return new String(bytes, charsetName); } catch (final UnsupportedEncodingException uee) { throw new EncryptionCodecException("Unable to convert the byte array into a String.", uee); } } /** * Encodes the string into a sequence of bytes using the named charset, storing the result into a new byte * array. *

* The behavior of this method when this string cannot be encoded in the given charset is unspecified. The {@link * java.nio.charset.CharsetEncoder} class should be used when more control over the encoding process is required. * @param string The String to convert to a byte array. * @param charsetName The name of a supported {@link java.nio.charset.Charset charset} * @return The resultant byte array * @throws EncryptionCodecException If the named charset is not supported * @throws NullPointerException if either string or charsetName is null */ public byte[] toBytes(final String string, final String charsetName) throws EncryptionCodecException { try { return string.getBytes(charsetName); } catch (final UnsupportedEncodingException uee) { throw new EncryptionCodecException("Unable to convert the String into a byte array.", uee); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy