// 
// Decompiled by Procyon v0.6.0
// 

package com.google.crypto.tink;

import com.google.crypto.tink.internal.Util;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;

public final class TinkJsonProtoKeysetFormat
{
    public static KeysetHandle parseKeyset(final String serializedKeyset, final SecretKeyAccess access) throws GeneralSecurityException {
        if (access == null) {
            throw new NullPointerException("SecretKeyAccess cannot be null");
        }
        try {
            return CleartextKeysetHandle.read(JsonKeysetReader.withString(serializedKeyset));
        }
        catch (final IOException e) {
            throw new GeneralSecurityException("Parse keyset failed");
        }
    }
    
    public static String serializeKeyset(final KeysetHandle keysetHandle, final SecretKeyAccess access) throws GeneralSecurityException {
        if (access == null) {
            throw new NullPointerException("SecretKeyAccess cannot be null");
        }
        try {
            final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            CleartextKeysetHandle.write(keysetHandle, JsonKeysetWriter.withOutputStream(outputStream));
            return new String(outputStream.toByteArray(), Util.UTF_8);
        }
        catch (final IOException e) {
            throw new GeneralSecurityException("Serialize keyset failed");
        }
    }
    
    public static KeysetHandle parseKeysetWithoutSecret(final String serializedKeyset) throws GeneralSecurityException {
        try {
            return KeysetHandle.readNoSecret(JsonKeysetReader.withString(serializedKeyset));
        }
        catch (final IOException e) {
            throw new GeneralSecurityException("Parse keyset failed");
        }
    }
    
    public static String serializeKeysetWithoutSecret(final KeysetHandle keysetHandle) throws GeneralSecurityException {
        try {
            final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            keysetHandle.writeNoSecret(JsonKeysetWriter.withOutputStream(outputStream));
            return new String(outputStream.toByteArray(), Util.UTF_8);
        }
        catch (final IOException e) {
            throw new GeneralSecurityException("Serialize keyset failed");
        }
    }
    
    public static KeysetHandle parseEncryptedKeyset(final String serializedEncryptedKeyset, final Aead keysetEncryptionAead, final byte[] associatedData) throws GeneralSecurityException {
        try {
            return KeysetHandle.readWithAssociatedData(JsonKeysetReader.withString(serializedEncryptedKeyset), keysetEncryptionAead, associatedData);
        }
        catch (final IOException e) {
            throw new GeneralSecurityException("Parse keyset failed");
        }
    }
    
    public static String serializeEncryptedKeyset(final KeysetHandle keysetHandle, final Aead keysetEncryptionAead, final byte[] associatedData) throws GeneralSecurityException {
        try {
            final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            keysetHandle.writeWithAssociatedData(JsonKeysetWriter.withOutputStream(outputStream), keysetEncryptionAead, associatedData);
            return new String(outputStream.toByteArray(), Util.UTF_8);
        }
        catch (final IOException e) {
            throw new GeneralSecurityException("Serialize keyset failed");
        }
    }
    
    private TinkJsonProtoKeysetFormat() {
    }
}
