parse output and check if a change

M

mike

Hi,

I am sending a command to unix and then I parse the output. The output is stored in a Info object ( Info.java). This will be used later in application..

The problem is that the output from the unix command might change. So I want to make sure that if there is a change then we parse and update the Info object. But if there is no change then we do not need to parse and extract the information. We can use the Info object directly.

The idea I have is to use the output from the command ( a text string) and calculate some kind of hash/md5sum.

So the next time I run the unix command I take the output (a string) and calculate hash/md5 and see if it is an exact match as was generated the firsttime.

Anyone will to give a hint on how to implememt this using java?

br,

//mike
 
A

Arne Vajhøj

I am sending a command to unix and then I parse the output. The
output is stored in a Info object ( Info.java). This will be used
later in application.

The problem is that the output from the unix command might change. So
I want to make sure that if there is a change then we parse and
update the Info object. But if there is no change then we do not need
to parse and extract the information. We can use the Info object
directly.

The idea I have is to use the output from the command ( a text
string) and calculate some kind of hash/md5sum.

So the next time I run the unix command I take the output (a string)
and calculate hash/md5 and see if it is an exact match as was
generated the first time.

Anyone will to give a hint on how to implememt this using java?

You will need to code your logic, but hashing a String is easy:

MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hash = md.digest(data.getBytes());

or maybe:

MessageDigest md = MessageDigest.getInstance("SHA-256");
String hash = toHex(md.digest(data.getBytes()));

....

private static String toHex(byte[] ba) {
char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < ba.length; i++) {
sb.append(hexdigit[(ba >> 4) & 0x0F]);
sb.append(hexdigit[ba & 0x0F]);
}
return sb.toString();
}

Arne
 
A

Arne Vajhøj

I am sending a command to unix and then I parse the output. The
output is stored in a Info object ( Info.java). This will be used
later in application.

The problem is that the output from the unix command might change. So
I want to make sure that if there is a change then we parse and
update the Info object. But if there is no change then we do not need
to parse and extract the information. We can use the Info object
directly.

The idea I have is to use the output from the command ( a text
string) and calculate some kind of hash/md5sum.

So the next time I run the unix command I take the output (a string)
and calculate hash/md5 and see if it is an exact match as was
generated the first time.

Anyone will to give a hint on how to implememt this using java?

You will need to code your logic, but hashing a String is easy:

MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hash = md.digest(data.getBytes());

or maybe:

MessageDigest md = MessageDigest.getInstance("SHA-256");
String hash = toHex(md.digest(data.getBytes()));

...

private static String toHex(byte[] ba) {
char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < ba.length; i++) {
sb.append(hexdigit[(ba >> 4) & 0x0F]);
sb.append(hexdigit[ba & 0x0F]);
}
return sb.toString();
}


MD5 is obsolete from a cryptographic point of view, so I
switched to SHA-256.

Arne
 
J

Joerg Meier

private static String toHex(byte[] ba) {
char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < ba.length; i++) {
sb.append(hexdigit[(ba >> 4) & 0x0F]);
sb.append(hexdigit[ba & 0x0F]);
}
return sb.toString();
}


For future reference:

DatatypeConverter.printHexBinary(ba).toLowerCase();

Liebe Gruesse,
Joerg
 
A

Arne Vajhøj

private static String toHex(byte[] ba) {
char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < ba.length; i++) {
sb.append(hexdigit[(ba >> 4) & 0x0F]);
sb.append(hexdigit[ba & 0x0F]);
}
return sb.toString();
}


For future reference:

DatatypeConverter.printHexBinary(ba).toLowerCase();


Ah - my little toHex is no longer needed.

Thanks.

Arne
 
D

Daniel Pitts

Hi,

I am sending a command to unix and then I parse the output. The output is stored in a Info object ( Info.java). This will be used later in application.

The problem is that the output from the unix command might change. So I want to make sure that if there is a change then we parse and update the Info object. But if there is no change then we do not need to parse and extract the information. We can use the Info object directly.

The idea I have is to use the output from the command ( a text string) and calculate some kind of hash/md5sum.

So the next time I run the unix command I take the output (a string) and calculate hash/md5 and see if it is an exact match as was generated the first time.

Anyone will to give a hint on how to implememt this using java?

br,

//mike
So, you're going to run a process every time (which is expensive), and
then avoid parsing the result (which is likely far less expensive) if
the output doesn't change?

Is there perhaps a better way to get this Info that doesn't involve
running an external program?
 
K

Kevin McMurtrie

Arne Vajhøj said:
I am sending a command to unix and then I parse the output. The
output is stored in a Info object ( Info.java). This will be used
later in application.

The problem is that the output from the unix command might change. So
I want to make sure that if there is a change then we parse and
update the Info object. But if there is no change then we do not need
to parse and extract the information. We can use the Info object
directly.

The idea I have is to use the output from the command ( a text
string) and calculate some kind of hash/md5sum.

So the next time I run the unix command I take the output (a string)
and calculate hash/md5 and see if it is an exact match as was
generated the first time.

Anyone will to give a hint on how to implememt this using java?

You will need to code your logic, but hashing a String is easy:

MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hash = md.digest(data.getBytes());

or maybe:

MessageDigest md = MessageDigest.getInstance("SHA-256");
String hash = toHex(md.digest(data.getBytes()));

...

private static String toHex(byte[] ba) {
char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < ba.length; i++) {
sb.append(hexdigit[(ba >> 4) & 0x0F]);
sb.append(hexdigit[ba & 0x0F]);
}
return sb.toString();
}


MD5 is obsolete from a cryptographic point of view, so I
switched to SHA-256.

Arne


I would have just saved the original String if it's not too big.
 
A

Arne Vajhøj

Arne Vajhøj said:
On 3/7/2013 2:10 PM, mike wrote:
I am sending a command to unix and then I parse the output. The
output is stored in a Info object ( Info.java). This will be used
later in application.

The problem is that the output from the unix command might change. So
I want to make sure that if there is a change then we parse and
update the Info object. But if there is no change then we do not need
to parse and extract the information. We can use the Info object
directly.

The idea I have is to use the output from the command ( a text
string) and calculate some kind of hash/md5sum.

So the next time I run the unix command I take the output (a string)
and calculate hash/md5 and see if it is an exact match as was
generated the first time.

Anyone will to give a hint on how to implememt this using java?

You will need to code your logic, but hashing a String is easy:

MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hash = md.digest(data.getBytes());

or maybe:

MessageDigest md = MessageDigest.getInstance("SHA-256");
String hash = toHex(md.digest(data.getBytes()));

...

private static String toHex(byte[] ba) {
char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < ba.length; i++) {
sb.append(hexdigit[(ba >> 4) & 0x0F]);
sb.append(hexdigit[ba & 0x0F]);
}
return sb.toString();
}


MD5 is obsolete from a cryptographic point of view, so I
switched to SHA-256.


I would have just saved the original String if it's not too big.


That is the simple solution.

:)

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top