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

com.github.isaichkindanila.crypt.lib.util.EncryptionChecker Maven / Gradle / Ivy

The newest version!
package com.github.isaichkindanila.crypt.lib.util;

import java.io.*;
import java.util.Arrays;

/**
 * Utility class used to determine whether or not file or stream is encrypted.
 */
public class EncryptionChecker {
    private static final byte[] ENCRYPTION_SIGNATURE = {
            0x43, 0x00,
            0x72, 0x11,
            0x79, 0x22,
            0x70, 0x33,
            0x74, 0x44,
            0x4c, 0x55,
            0x69, 0x66,
            0x62, 0x77
    };

    private EncryptionChecker() {
    }

    /**
     * Returns a copy of encryption signature.
     *
     * @return copy of signature
     */
    public static byte[] getEncryptionSignature() {
        byte[] bytes = new byte[ENCRYPTION_SIGNATURE.length];
        System.arraycopy(ENCRYPTION_SIGNATURE, 0, bytes, 0, bytes.length);

        return bytes;
    }

    /**
     * Checks if file starts with encryption signature.
     * 

* Returns false if {@code IOException} occurs. * * @param file file to check * @return true if the file exists, is not a directory and it's first bytes match the encryption signature */ public static boolean isFileEncrypted(File file) { if (!file.exists() || file.isDirectory()) { return false; } try (InputStream in = new FileInputStream(file)) { byte[] signature = new byte[ENCRYPTION_SIGNATURE.length]; if (in.read(signature) == signature.length) { return Arrays.equals(signature, ENCRYPTION_SIGNATURE); } else { return false; } } catch (IOException e) { return false; } } /** * Checks if {@code BufferedInputStream} starts with encryption signature. *

* Returns false if {@code IOException} occurs. * * @param in stream to check * @return true if first bytes of stream match the encryption signature */ @SuppressWarnings("ResultOfMethodCallIgnored") public boolean isStreamEncrypted(BufferedInputStream in) { byte[] bytes = new byte[ENCRYPTION_SIGNATURE.length]; try { in.mark(bytes.length); in.read(bytes); in.reset(); return Arrays.equals(bytes, ENCRYPTION_SIGNATURE); } catch (IOException e) { return false; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy