CryptoSupportImpl.java 5.9 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.granite.crypto.internal;

import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import java.util.Arrays;

public abstract class CryptoSupportImpl
implements CryptoSupport {
    private static final String CHARACTER_SET = "UTF-8";

    protected CryptoSupportImpl() {
    }

    protected abstract void init(byte[] var1) throws Exception;

    protected abstract void init_hmac(byte[] var1) throws Exception;

    protected abstract void dispose(byte[] var1);

    protected abstract void dispose();

    protected abstract byte[] init() throws Exception;

    protected abstract byte[] init_hmac() throws Exception;

    protected abstract byte[] getCipherText(byte[] var1) throws Exception;

    protected abstract byte[] getCipherText(byte[] var1, byte[] var2) throws Exception;

    protected abstract byte[] getPlainText(byte[] var1) throws Exception;

    protected abstract byte[] getPlainText(byte[] var1, byte[] var2) throws Exception;

    @Override
    public final byte[] encrypt(byte[] plainText) throws CryptoException {
        try {
            return this.getCipherText(plainText);
        }
        catch (Exception e) {
            throw new CryptoException("Failed encrypting plain text", e);
        }
    }

    @Override
    public final byte[] decrypt(byte[] cipherText) throws CryptoException {
        try {
            return this.getPlainText(cipherText);
        }
        catch (Exception e) {
            throw new CryptoException("Failed decrypting cipher text", e);
        }
    }

    @Override
    public final byte[] encrypt(byte[] key, byte[] plainText) throws CryptoException {
        try {
            return this.getCipherText(key, plainText);
        }
        catch (Exception e) {
            throw new CryptoException("Failed encrypting plain text", e);
        }
    }

    @Override
    public final byte[] decrypt(byte[] key, byte[] cipherText) throws CryptoException {
        try {
            return this.getPlainText(key, cipherText);
        }
        catch (Exception e) {
            throw new CryptoException("Failed decrypting cipher text", e);
        }
    }

    @Override
    public final boolean isProtected(String text) {
        return text != null && text.length() > 2 && text.charAt(0) == '{' && text.charAt(text.length() - 1) == '}' && text.length() % 2 == 0;
    }

    @Override
    public final String protect(String plainText) throws CryptoException {
        byte[] datumBytes = null;
        byte[] cipherBytes = null;
        try {
            datumBytes = plainText.getBytes("UTF-8");
            cipherBytes = this.encrypt(datumBytes);
            String string = CryptoSupportImpl.toString(cipherBytes);
            return string;
        }
        catch (Exception e) {
            throw new CryptoException("Cannot encrypt plain text", e);
        }
        finally {
            CryptoSupportImpl.clear(datumBytes);
            CryptoSupportImpl.clear(cipherBytes);
        }
    }

    @Override
    public final String protect(byte[] key, String plainText) throws CryptoException {
        byte[] datumBytes = null;
        byte[] cipherBytes = null;
        try {
            datumBytes = plainText.getBytes("UTF-8");
            cipherBytes = this.encrypt(key, datumBytes);
            String string = CryptoSupportImpl.toString(cipherBytes);
            return string;
        }
        catch (Exception e) {
            throw new CryptoException("Cannot encrypt plain text", e);
        }
        finally {
            CryptoSupportImpl.clear(datumBytes);
            CryptoSupportImpl.clear(cipherBytes);
        }
    }

    @Override
    public final String unprotect(String cipherText) throws CryptoException {
        byte[] datumBytes = null;
        byte[] cipherBytes = null;
        try {
            cipherBytes = this.toByteArray(cipherText);
            datumBytes = this.decrypt(cipherBytes);
            String string = new String(datumBytes, "UTF-8");
            return string;
        }
        catch (Exception e) {
            throw new CryptoException("Cannot convert byte data", e);
        }
        finally {
            CryptoSupportImpl.clear(datumBytes);
            CryptoSupportImpl.clear(cipherBytes);
        }
    }

    @Override
    public final String unprotect(byte[] key, String cipherText) throws CryptoException {
        byte[] datumBytes = null;
        byte[] cipherBytes = null;
        try {
            cipherBytes = this.toByteArray(cipherText);
            datumBytes = this.decrypt(key, cipherBytes);
            String string = new String(datumBytes, "UTF-8");
            return string;
        }
        catch (Exception e) {
            throw new CryptoException("Cannot convert byte data", e);
        }
        finally {
            CryptoSupportImpl.clear(datumBytes);
            CryptoSupportImpl.clear(cipherBytes);
        }
    }

    protected static void clear(byte[] data) {
        if (data != null) {
            Arrays.fill(data, 0);
        }
    }

    private static String toString(byte[] binaryData) {
        StringBuilder res = new StringBuilder(binaryData.length * 2 + 2);
        res.append('{');
        for (int i = 0; i < binaryData.length; ++i) {
            String hex = Integer.toHexString(binaryData[i] & 255);
            if (hex.length() < 2) {
                res.append('0');
            }
            res.append(hex);
        }
        res.append('}');
        return res.toString();
    }

    private byte[] toByteArray(String text) {
        if (!this.isProtected(text)) {
            throw new IllegalArgumentException("Cannot decrypt obviously unprotected data");
        }
        byte[] data = new byte[text.length() / 2 - 1];
        for (int i = 1; i < text.length() - 1; i += 2) {
            String hex = text.substring(i, i + 2);
            int val = Integer.parseInt(hex, 16);
            data[i / 2] = (byte)val;
        }
        return data;
    }
}