Solved

Automate SHA-256 process

Posted on 2013-05-30
6
567 Views
Last Modified: 2013-05-31
We have an XML file which is produced weekly.
Within that file are the tags <BACSHashCode>characterstring</BACSHashCode>

The tags appear multiple times within the XML file and the character string differs within each occurrence.

I need to be able to encrypt each character string occurrence with a SHA-256 encrypted version. I am doing this manually at present using an online facility. I copy the value of each 'character string' from the file into conversion facility and paste back the result overriding the previous character string.

It is a very lengthy and error-prone process.

Does anyone know of a utility which will automate this process?

I guess I would need to feed it the file name, or open the file within the utility. Then I would need to give it the name of the tags and it would do the job for me.

Thanks for any suggestions.
0
Comment
Question by:RichardWalsh
  • 4
  • 2
6 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 39206906
If you have perl available to you (or can install it).  It's a one-liner:

perl -i.bak -ne 'BEGIN { use Digest::SHA qw(sha256_hex); } s#<BACSHashCode>(.*?)</BACSHashCode>#qq(<BACSHashCode>).sha256_hex($1).qq(</BACSHashCode>)#ge; print;' myfile.xml

The above would make a backup of the file called myfile.xml called myfile.xml.bak and in-place edit the original file (myfile.xml) swapping out all instances of <BACSHashCode>whateverr</BACSHashCode> with the SHA256 version.  I wrote it windows friendly, so it should work under both windows and *nixes.  Just change the single quotes to double quotes on windows.  Windows version:

perl -i.bak -ne "BEGIN { use Digest::SHA qw(sha256_hex); } s#<BACSHashCode>(.*?)</BACSHashCode>#qq(<BACSHashCode>).sha256_hex($1).qq(</BACSHashCode>)#ge; print;" myfile.xml
 
An example of its use:
markh@wheeze:~> cat myfile.xml
<data>
   <element>
      <atag>whatever</atag>
      <BACSHashCode>hello there</BACSHashCode>
    </element>
   <element>
      <atag>another element</atag>
      <BACSHashCode>more data</BACSHashCode>
    </element>
</data>

markh@wheeze:~> perl -i.bak -ne 'BEGIN { use Digest::SHA qw(sha256_hex); } s#<BACSHashCode>(.*?)</BACSHashCode>#qq(<BACSHashCode>).sha256_hex($1).qq(</BACSHashCode>)#ge; print;' myfile.xml 
markh@wheeze:~> cat myfile.xml
<data>
   <element>
      <atag>whatever</atag>
      <BACSHashCode>12998c017066eb0d2a70b94e6ed3192985855ce390f321bbdb832022888bd251</BACSHashCode>
    </element>
   <element>
      <atag>another element</atag>
      <BACSHashCode>737165b08ad9b72940af2167aae90fb7eb3b52faf641c0590d36f857adbe451d</BACSHashCode>
    </element>
</data>

Open in new window

0
 

Author Comment

by:RichardWalsh
ID: 39206952
Many thanks for your very quick response.  I am out today but will try it out this evening and let you know how it goes.
Richard
0
 

Author Comment

by:RichardWalsh
ID: 39209271
I downloaded Perl to my Windows laptop and your solution worked first time!

I am SO grateful for this. The (old) version of the programming language I am using (Progress/Openedge) does not support SHA-256 - although the latest versions do support it.
So we were going to have to manually generate the values in several files weekly.

Your program has made the job MUCH simpler.

Thanks a million.
Richard Walsh
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 25

Expert Comment

by:clockwatcher
ID: 39209904
No problem.  Glad to have been of help.  Funny that you mention Progress.  We actually just implemented a system called QAD that sits atop a Progress/OpenEdge system and I've got to get up to speed with programming in Progress.
0
 

Author Comment

by:RichardWalsh
ID: 39211811
I know of QAD...... I believe it's a big system. If my memory is correct the company was started by a woman and it made her the wealthiest (or one of the wealthiest) women the software world has produced.

If you've not come across Progress before I think you'll be surprised how capable the 4GL is (now called ABL I think).

Progress has always had a reputation for being technically very competent and very profitable but preferring 'stealth marketing' compared with people like Oracle etc.

Good luck and thanks again.
0
 

Author Closing Comment

by:RichardWalsh
ID: 39211818
This solution really went 'the extra mile' in providing a worked example of how to use the program. It has helped me enormously.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question