Skip to Main Content
  • Questions
  • Using Java code in PLSQL to use checksum function

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Yashasvi.

Asked: January 14, 2019 - 10:13 am UTC

Last updated: January 15, 2019 - 11:35 am UTC

Version: Oracle 12c

Viewed 1000+ times

You Asked

Hi Tom,

Thanks for being with us and all your support.

Suppose I have a File Name :- "MPR_YES_CDC_11122018_V1.csv"
Checksum of the given file as per below java code :- cab5f886bba15048a6f7180665871a2fa4ad4917

In the same manner I have to use below java code into my PLSQL code to get checksum of the given file name, I have tried using
select standard_hash ('MPR_YES_CDC_11122018_V1_.csv', 'SHA1' )
from dual but the output of checksum is not matching with the output of the java checksum.

Is there anyway by which we can use below java code into my PLSQL code.

private String generateChecksum() {
                                //Create checksum for this file
                                File file = new File("D:\\MPR_FILES\\MPR_YES_CDC_11122018_V1_cab5f886bba15048a6f7180665871a2fa4ad4917.csv");
                                
                                //Get the checksum
                                String checksum = getFileChecksum(file);
                                
                                //see checksum
                                System.out.println(checksum);
                                return checksum;
                }
                
private String getFileChecksum(File file)
                {
                    StringBuilder sb = new StringBuilder();
                                try {
                                                //Use SHA-1 algorithm
                                                MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
                                    //Get file input stream for reading the file content
                                    FileInputStream fis = new FileInputStream(file);
                                     
                                    //Create byte array to read data in chunks
                                    byte[] byteArray = new byte[1024];
                                    int bytesCount = 0;
                                      
                                    //Read file data and update in message digest
                                    while ((bytesCount = fis.read(byteArray)) != -1) {
                                                messageDigest.update(byteArray, 0, bytesCount);
                                    };
                                     
                                    //close the stream; We don't need it now.
                                    fis.close();
                                     
                                    //Get the hash's bytes
                                    byte[] bytes = messageDigest.digest();
                                     
                                    //This bytes[] has bytes in decimal format;
                                    //Convert it to hexadecimal format
                                    for(int i=0; i< bytes.length ;i++)
                                    {
                                        sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
                                    }
                                } catch (Exception e) {
                                                System.out.println("Exception : "+ e.getMessage());
                                }
                    
                    //return complete hash
                   return sb.toString();
                }                              

and Chris said...

You can load a Java class into the database. Then put a PL/SQL wrapper around it. You can then call this in the database.

You can find a worked example at:

https://docs.oracle.com/en/database/oracle/oracle-database/12.2/jjdev/developing-Java-stored-procedures.html#GUID-AE1E5C4B-A077-4D1E-8821-6A7142BF1FEA

PS - the Java code appears to be building a checksum based on the contents of the file. But your SQL example only hashes the filename! So these are bound to be different.

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

built in

Racer I., January 15, 2019 - 8:33 am UTC

Hi,

Here https://stackoverflow.com/questions/35574602/oracle-standard-hash-not-available-in-plsql
someone says : For text, STANDARD_HASH is the same as DBMS_CRYPTO.HASH with SHA1:
  dbms_output.put_line(dbms_crypto.hash(cast('test' as clob), dbms_crypto.hash_sh1));

Both can take CLOBs which you should be able to read via DBMS_LOB.LOADCLOBFROMFILE
https://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_lob.htm#i998978

caveats :
- haven't tried this myself
- your java hash-algorithm might not be sha1
- the file is read completely into memory instead of being processed piecewise (but so does your java code)

regards
Chris Saxon
January 15, 2019 - 11:35 am UTC

Nice, thanks for sharing.

#loloftheday

A reader, January 18, 2019 - 8:37 pm UTC


More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library