Solved

Automate SHA-256 process

Posted on 2013-05-30
6
556 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 25

Expert Comment

by:clockwatcher
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suppose you are a shopaholic and you shop online frequently from a website. That means that is obvious that you would have been registered yourself on that website. Now, once in a while that website that you always buy from becomes a victim of phish…
There are many reasons malware will stay around and continue to grow as a business.  The biggest reason is the expanding customer base.  More than 40% of people who are infected with ransomware, pay the ransom.  That makes ransomware a multi-million…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now