• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 584
  • Last Modified:

Automate SHA-256 process

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
RichardWalsh
Asked:
RichardWalsh
  • 4
  • 2
1 Solution
 
clockwatcherCommented:
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
 
RichardWalshAuthor Commented:
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
 
RichardWalshAuthor Commented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
clockwatcherCommented:
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
 
RichardWalshAuthor Commented:
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
 
RichardWalshAuthor Commented:
This solution really went 'the extra mile' in providing a worked example of how to use the program. It has helped me enormously.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now