// Path: ESignCore/src/main/java/tr/gov/turkiye/esign/exception/SmartCardException.java
// public class SmartCardException extends Exception {
// 
//     public SmartCardException(String s, Throwable t) {
//         super(s, t);
//     }
// }
// 
// Path: ESignCore/src/main/java/tr/gov/turkiye/esign/support/ExceptionSupport.java
// public class ExceptionSupport {
// 
//     private static ResourceBundle bundle = null;
// 
//     public static String getValue(final String key) {
//         if (bundle == null) {
//             Locale locale = new Locale("tr", "TR");
//             bundle = ResourceBundle.getBundle("exception/SmartCardExceptions", locale);
//         }
//         return bundle.getString(key);
//     }
//     
// }


import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import tr.gov.turkiye.esign.exception.SmartCardException;
import tr.gov.turkiye.esign.support.ExceptionSupport;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tr.gov.turkiye.esign.util;


/**
 *
 * @author sercan
 */
public class Util {

    /**
     * Gets bytes of file
     *
     * @param fileName
     * @return bytes of file.
     * @throws FileNotFoundException
     * @throws NoSuchAlgorithmException
     * @throws IOException
     */
    public static byte[] getContent(String fileName) throws FileNotFoundException, NoSuchAlgorithmException, IOException {
        InputStream dataInputStream = new FileInputStream(fileName);

        // we buffer the content to have it after hashing for the PKCS#7 content
        ByteArrayOutputStream contentBuffer = new ByteArrayOutputStream();
        byte[] dataBuffer = new byte[1024];
        int bytesRead;

        // feed all data from the input stream to the message digest
        while ((bytesRead = dataInputStream.read(dataBuffer)) >= 0) {
            // and buffer the data
            contentBuffer.write(dataBuffer, 0, bytesRead);
        }
        contentBuffer.close();
        return contentBuffer.toByteArray();

    }

    /**
     * writes bytes to file.
     *
     * @param fileName
     * @param content
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void writeContent(String fileName, byte[] content) throws FileNotFoundException, IOException {
        OutputStream signatureOutput = new FileOutputStream(fileName);
        signatureOutput.write(content);
        signatureOutput.flush();
        signatureOutput.close();
    }

    /**
     * Performs a final update on the digest using the specified array of bytes,
     * then completes the digest computation.
     *
     * @param input the input to be updated before the digest is completed.
     * @return the array of bytes for the resulting hash value
     * @throws tr.gov.turkiye.esign.exception.SmartCardException
     */
// Path: ESignCore/src/main/java/tr/gov/turkiye/esign/exception/SmartCardException.java
// public class SmartCardException extends Exception {
// 
//     public SmartCardException(String s, Throwable t) {
//         super(s, t);
//     }
// }
// 
// Path: ESignCore/src/main/java/tr/gov/turkiye/esign/support/ExceptionSupport.java
// public class ExceptionSupport {
// 
//     private static ResourceBundle bundle = null;
// 
//     public static String getValue(final String key) {
//         if (bundle == null) {
//             Locale locale = new Locale("tr", "TR");
//             bundle = ResourceBundle.getBundle("exception/SmartCardExceptions", locale);
//         }
//         return bundle.getString(key);
//     }
//     
// }


// Path: ESignCore/src/main/java/tr/gov/turkiye/esign/util/Util.java
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import tr.gov.turkiye.esign.exception.SmartCardException;
import tr.gov.turkiye.esign.support.ExceptionSupport;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tr.gov.turkiye.esign.util;


/**
 *
 * @author sercan
 */
public class Util {

    /**
     * Gets bytes of file
     *
     * @param fileName
     * @return bytes of file.
     * @throws FileNotFoundException
     * @throws NoSuchAlgorithmException
     * @throws IOException
     */
    public static byte[] getContent(String fileName) throws FileNotFoundException, NoSuchAlgorithmException, IOException {
        InputStream dataInputStream = new FileInputStream(fileName);

        // we buffer the content to have it after hashing for the PKCS#7 content
        ByteArrayOutputStream contentBuffer = new ByteArrayOutputStream();
        byte[] dataBuffer = new byte[1024];
        int bytesRead;

        // feed all data from the input stream to the message digest
        while ((bytesRead = dataInputStream.read(dataBuffer)) >= 0) {
            // and buffer the data
            contentBuffer.write(dataBuffer, 0, bytesRead);
        }
        contentBuffer.close();
        return contentBuffer.toByteArray();

    }

    /**
     * writes bytes to file.
     *
     * @param fileName
     * @param content
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void writeContent(String fileName, byte[] content) throws FileNotFoundException, IOException {
        OutputStream signatureOutput = new FileOutputStream(fileName);
        signatureOutput.write(content);
        signatureOutput.flush();
        signatureOutput.close();
    }

    /**
     * Performs a final update on the digest using the specified array of bytes,
     * then completes the digest computation.
     *
     * @param input the input to be updated before the digest is completed.
     * @return the array of bytes for the resulting hash value
     * @throws tr.gov.turkiye.esign.exception.SmartCardException
     */
    public static byte[] digestSHA256(byte[] input) throws SmartCardException {