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

package com.google.crypto.tink;

import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;

public final class TinkProtoKeysetFormat
{
    public static KeysetHandle parseKeyset(final byte[] serializedKeyset, final SecretKeyAccess access) throws GeneralSecurityException {
        if (access == null) {
            throw new NullPointerException("SecretKeyAccess cannot be null");
        }
        try {
            return CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(serializedKeyset));
        }
        catch (final IOException e) {
            throw new GeneralSecurityException("Parse keyset failed");
        }
    }
    
    public static byte[] 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, BinaryKeysetWriter.withOutputStream(outputStream));
            return outputStream.toByteArray();
        }
        catch (final IOException e) {
            throw new GeneralSecurityException("Serialize keyset failed");
        }
    }
    
    public static KeysetHandle parseKeysetWithoutSecret(final byte[] serializedKeyset) throws GeneralSecurityException {
        return KeysetHandle.readNoSecret(serializedKeyset);
    }
    
    public static byte[] serializeKeysetWithoutSecret(final KeysetHandle keysetHandle) throws GeneralSecurityException {
        try {
            final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            keysetHandle.writeNoSecret(BinaryKeysetWriter.withOutputStream(outputStream));
            return outputStream.toByteArray();
        }
        catch (final IOException e) {
            throw new GeneralSecurityException("Serialize keyset failed");
        }
    }
    
    public static KeysetHandle parseEncryptedKeyset(final byte[] serializedEncryptedKeyset, final Aead keysetEncryptionAead, final byte[] associatedData) throws GeneralSecurityException {
        try {
            return KeysetHandle.readWithAssociatedData(BinaryKeysetReader.withBytes(serializedEncryptedKeyset), keysetEncryptionAead, associatedData);
        }
        catch (final IOException e) {
            throw new GeneralSecurityException("Parse keyset failed");
        }
    }
    
    public static byte[] serializeEncryptedKeyset(final KeysetHandle keysetHandle, final Aead keysetEncryptionAead, final byte[] associatedData) throws GeneralSecurityException {
        try {
            final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            keysetHandle.writeWithAssociatedData(BinaryKeysetWriter.withOutputStream(outputStream), keysetEncryptionAead, associatedData);
            return outputStream.toByteArray();
        }
        catch (final IOException e) {
            throw new GeneralSecurityException("Serialize keyset failed");
        }
    }
    
    private TinkProtoKeysetFormat() {
    }
}
