Aero Wang
Aero Wang

Reputation: 9247

How to use php's SHA1 function to generate a list of hash/Base64 outputs?

I want to use PHP's SHA1 function to generate a list of hash/Base64 outputs from a single string.

For example, if I have a string, "ABCDE"

Then I want to generate 5 outputs like:

echo sha1('ABCDE');
echo sha1('BCDE');
echo sha1('CDE');
echo sha1('DE');
echo sha1('E');

And the number of outputs will match the length of the string. How would I accomplish this?

Upvotes: 1

Views: 30

Answers (2)

R. Chappell
R. Chappell

Reputation: 1282

It was annoying me, this is the easiest by far.

$str = 'ABCDE';
while (strlen($str)) {
    echo sha1($str);
    $str = substr($str, 1);
}

Upvotes: 0

James Wayne
James Wayne

Reputation: 1912

<?php
$s = 'ABCDE';
$t = strlen($s);
for ($i = 1; $i <= $t; $i++) {
  echo sha1($s);
  echo '</br>';
  $s = substr($s, 1);
}
?>

I use a for loop but you can use while too. I don't know how to.

Upvotes: 1

Related Questions