On Fri, 16 Oct 2020 18:53:39 +0000, Sasso, Len <[email protected]> wrote:

>Anyone know if a Checksum/Md5 file can be created on a IBM Mainframe?
Yes.


>If so, how?

package com.jantje.checksum;

import com.ibm.jzos.FileFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class MD5CheckSum {

    public static byte[] createChecksum(String filename) throws IOException, 
NoSuchAlgorithmException {

        InputStream inputStream = FileFactory.newInputStream(filename);
        //InputStream inputStream = new FileInputStream(filename);

        byte[] buffer = new byte[1024];
        MessageDigest complete = MessageDigest.getInstance("MD5");
        int numRead;
        do {
            numRead = inputStream.read(buffer);
            if (numRead > 0) {
                complete.update(buffer, 0, numRead);
            }
        } while (numRead != -1);
        inputStream.close();
        return complete.digest();
    }

    public static String getMD5Checksum(String filename) throws IOException, 
NoSuchAlgorithmException {
        byte[] b = createChecksum(filename);
        String result = "";
        for (int i = 0; i < b.length; i++) {
            result +=
                    Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
        }
        return result;
    }

    public static void getMD5CheckSums(String parmFileName) throws IOException, 
NoSuchAlgorithmException {

        BufferedReader fileNames = FileFactory.newBufferedReader(parmFileName);
        try {
            String datasetName;
            while ((datasetName = fileNames.readLine()) != null) {
                System.out.print(String.format("%1$-50s", datasetName));
                System.out.println(getMD5Checksum(datasetName));
            }
        } finally {
            fileNames.close();
        }
    }

    public static void main(String args[]) {
        try {
            getMD5CheckSums(args[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Cheers,

Jantje.

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

Reply via email to