Recent

Author Topic: Store Console/Bash String Output to a Variable  (Read 4175 times)

allanregistos

  • Jr. Member
  • **
  • Posts: 54
  • In Christ Alone
Store Console/Bash String Output to a Variable
« on: May 06, 2016, 10:22:15 am »
Hello all,
I have this code that will call a bash terminal function:

Code: Pascal  [Select][+][-]
  1. S:=fpSystem('echo -n ''lazarus'' | sha1sum');  

Which has a sha1 output of:
Code: Pascal  [Select][+][-]
  1. 08c32476538739d399981496fc817f08c4c092e3

How can you store the sha1 output to a string variable?

I also tried to look at unit sha1, but I've found no function similar above.

Thank you...
Allan

God is my refuge and my strength.

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Store Console/Bash String Output to a Variable
« Reply #1 on: May 06, 2016, 10:38:20 am »
How can you store the sha1 output to a string variable?

I also tried to look at unit sha1, but I've found no function similar above.

I have no idea what you are talking about...
Code: Pascal  [Select][+][-]
  1. program sha1_on_file;
  2.  
  3. uses
  4.    sha1;
  5.  
  6. var
  7.   Hash : TSHA1Digest;
  8.   i    : integer;
  9.   S    : String;
  10.  
  11. begin
  12.   Hash := SHA1File('sha1_on_file.pas');
  13.  
  14.   S := '';
  15.   for i := Low(Hash) to High(Hash) do
  16.     WriteStr(S, S, HexStr(Hash[i],2));
  17.   Writeln('the hash = ', S);
  18. end.
  19.  

allanregistos

  • Jr. Member
  • **
  • Posts: 54
  • In Christ Alone
Re: Store Console/Bash String Output to a Variable
« Reply #2 on: May 06, 2016, 10:55:53 am »
Quote
I have no idea what you are talking about...

If you type this at bash shell in Linux
Code: Bash  [Select][+][-]
  1. echo -n 'lazarus' | sha1sum
you can get the sha1 value of the string lazarus as an output. If you are interested see attached file.
The problem I have is how can I store that output into a variable. Don't know how to do that.

Thank you for the sha1 tip, I do no need that shell command anymore.

Here's what I did from your code:

Code: Pascal  [Select][+][-]
  1. Hash := SHA1String('mystring');
  2.   S := '';
  3.   for i := Low(Hash) to High(Hash) do
  4.     WriteStr(S, S, HexStr(Hash[i],2));  
God is my refuge and my strength.

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Store Console/Bash String Output to a Variable
« Reply #3 on: May 06, 2016, 11:04:04 am »
As said, i have no idea what you were talking about ;-)

I mistakenly read that you wanted to calculate a sha1 checksum on the lazarus executable but , instead you wanted to have the checksum for the string "lazarus"

Glad that you figured it out  :-*


marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8722
  • FPC developer.
Re: Store Console/Bash String Output to a Variable
« Reply #4 on: May 06, 2016, 11:08:51 am »
If for some reason you want to do it via the commandline instead of a direct routine, you need to execute a shell to interpret the "pipe". This is what fpsystem does too.

Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. uses process;
  3.  
  4. var s : string;
  5. begin
  6.  if runcommand('/bin/bash',['-c','echo -n ''lazarus'' | sha1sum'],s) then
  7.   writeln('output:',s);
  8. end.
  9.  
  10.  

This assume the current directory in the running program is where the lazarus binary is (just like you cd to it using the shell before the command)

Strictly speaking you need to lookup the shell's name and location using the environment variable, but depending on the nature software, you can often assume bash to be there.


allanregistos

  • Jr. Member
  • **
  • Posts: 54
  • In Christ Alone
Re: Store Console/Bash String Output to a Variable
« Reply #5 on: May 07, 2016, 02:00:24 am »
Hi marcov,

Thanks for the tip, I will use it definitely for some projects. That's what I want originally. I need to store the bash output into a pascal variable so that I can use it to compare passwords. The reason why I need the bash terminal because I have difficulty of using DCrypt's Sha1 function. The built-in Sha1 unit saves my day actually.
God is my refuge and my strength.

allanregistos

  • Jr. Member
  • **
  • Posts: 54
  • In Christ Alone
Re: Store Console/Bash String Output to a Variable
« Reply #6 on: May 07, 2016, 02:03:09 am »
As said, i have no idea what you were talking about ;-)

I mistakenly read that you wanted to calculate a sha1 checksum on the lazarus executable but , instead you wanted to have the checksum for the string "lazarus"

Glad that you figured it out  :-*

Yes I need a hash string rather than hashing a file.
Thanks...
 ;)
God is my refuge and my strength.

 

TinyPortal © 2005-2018