12 Replies Latest reply on Oct 15, 2019 9:50 AM by Mani Ar

    SHA-256 Encryption using JTX

    Mani Ar Seasoned Veteran

      Hi,

       

      I am trying to do SHA256 Encryption for the input Email Address field and my mapping looks like as below in the image.

       

      My Java Code is given below (I have copied from Informatica forums)

      But, I am not seeing any records coming in the output file. (The file is empty)

       

      I don't see any errors on the log as well.

       

      Can anyone help me to point out where am I doing anything wrong here please?

       

      Thanks in Advance!

      Mani A

       

      My Java TX Code:

       

      On the Import Packages Tab, I copied the below import packages.

       

      import java.io.FileInputStream;

      import java.io.UnsupportedEncodingException;

      import java.math.BigInteger;

      import java.security.MessageDigest;

      import java.security.NoSuchAlgorithmException;

      import java.nio.charset.StandardCharsets;

       

      On the Input Row Tab, I copied the below code:

      EmailAddress is the Input Port

      Email_Address_Hashed is my output port

       

      StringBuffer sb = new StringBuffer();

       

        try {

        MessageDigest md1 = MessageDigest.getInstance("SHA-256")

       

            md1.update(EmailAddress.getBytes());

       

            byte byteData[] = md1.digest();

          

          //convert the byte to hex format method 1

       

               for (int i = 0; i < byteData.length; i++) {

                sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));

               }

       

         }

       

      catch (NoSuchAlgorithmException ne ) {

       

       

        throw new RuntimeException(ne);

       

       

        }

       

      Email_Address_Hashed = sb.toString();

       

      Thanks,

      Mani A

        • 1. Re: SHA-256 Encryption using JTX
          Nico Heinze Guru

          Basically I understand what your code tries to achieve, but this one line puzzles me:

           

          sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));

           

          What is this code supposed to to?

          I do understand what byteData[i]&0xff does, but what shall that +0x100 do? In conjunction with the substring(1) it looks to me as if first 0x100 is added to each byte in order to translate each byte into a hex string, and then the first hex digit (always a 1) is cut off. Why?

           

          Thanks,

          Nico

          • 2. Re: SHA-256 Encryption using JTX
            Mani Ar Seasoned Veteran

            Thank you Nico!

             

            I am not sure about what exactly the code does it. I copied from one of our forums for converting my email to sha256 encoded email.

             

            Please suggest if any other method is available to achieve the same.

             

            Thanks in Advance!

            • 3. Re: SHA-256 Encryption using JTX
              Nico Heinze Guru

              Do I understand right that you want the Email_Addr_Hex port (the name is cut off in the screenshot) to contain the MD5 value of the email address?

               

              Regards,

              Nico

              • 4. Re: SHA-256 Encryption using JTX
                Nico Heinze Guru

                Sorry, I meant that the Email_Addr_Hex port shall contain the SHA-256 value?

                • 5. Re: SHA-256 Encryption using JTX
                  Mani Ar Seasoned Veteran

                  Yes Nico!

                   

                  the Output port email_addr_hashed should contain the sha256 encoded email

                  • 6. Re: SHA-256 Encryption using JTX
                    Nico Heinze Guru

                    Will look after some similar code I've written in 2015 tomorrow.

                    If I don't return here by tomorrow evening, please ping me again.

                     

                    Thanks,

                    Nico

                    • 7. Re: SHA-256 Encryption using JTX
                      Mani Ar Seasoned Veteran

                      Sure Nico!

                       

                      Thanks again!

                       

                      My input file comes as a flat file and one of the fields is email address (plain english) and in the output, I should convert the email to SHA256 format.

                       

                      Thank you,

                      Mani A

                      • 8. Re: SHA-256 Encryption using JTX
                        Mani Ar Seasoned Veteran

                        Hi Nico,

                         

                        I just wanted to send a reminder as you asked me to send one this evening for this post pls.

                         

                        Thanks again,

                        Mani A

                        • 9. Re: SHA-256 Encryption using JTX
                          Nico Heinze Guru

                          Hi Mani,

                           

                          I haven't found the exact code from 2015, but what I did find was that the Java code should be correct IF everything goes smoothly. In general the code looks good to me.

                           

                          However, your Java code doesn't contain even the smallest bit of error checking. That's not very healthy. For example, the getInstance() method may throw an exception if the SHA-256 algorithm is not implemented by the Java Runtime Environment (JRE) which is part of the Informatica platform installation (and hence part of the PowerCenter installation), but this exception is the only exception caught in the Java code you've posted.

                           

                          If I were you, I would start by catching eand and every possible exception, for example by printing the complete stack trace of each exception to the session log. That will help you to find out why the code doesn't work.

                           

                          Regards,

                          Nico

                          • 10. Re: SHA-256 Encryption using JTX
                            Mani Ar Seasoned Veteran

                            Thank you Nico!

                             

                            I am not a Java person and thats why copied the code from the forum.

                             

                            In the meanwhile I got a much simpler version of sha-256 conversion and not sure whether that works fine. When I copied this code into my run time designer mapping JTX, it did not produce any output records either.

                             

                            This is the code I got

                             

                            1. try {
                            2.             MessageDigest md = MessageDigest.getInstance("SHA-1");
                            3.             byte[] messageDigest = md.digest(AUDIT_KEY.getBytes());
                            4.             BigInteger number = new BigInteger(1, messageDigest);
                            5.             String hashtext = number.toString(16);
                            6.             // Now we need to zero pad it if you actually want the full 32 chars.
                            7.             while (hashtext.length() < 32) {
                            8.                 hashtext = "0" + hashtext;
                            9.             }
                            10.             ENC_KEY= hashtext;
                            11.         }
                            12.         catch (NoSuchAlgorithmException e) {
                            13.             throw new RuntimeException(e);
                            14.         }

                             

                            Thanks,

                            Mani A

                            • 11. Re: SHA-256 Encryption using JTX
                              Nico Heinze Guru

                              No offense intended, really not: how much experience with Java do you have? A little? None at all?

                               

                              The point is: I can try to get the code running smoothly including error handling, but that will take time. Which I currently don't have. And I don't want to "block" you for a long time just because I'm busy. That's why I'm asking for your Java experience. A little hands-on experience is enough, you don't need to be a great GUI builder or anything like that.

                               

                              Regards,

                              Nico

                              • 12. Re: SHA-256 Encryption using JTX
                                Mani Ar Seasoned Veteran

                                Thank you Nico! No probs at all. Actually, my project is on hold as of today and we may be able to go with md5 itself