user2879090
user2879090

Reputation: 15

Convert PHP function to linux bash?

I have a php function that generates a rollingkey for a webAPI to download a xml file, this is literally the only reason i have PHP installed on my server and if it could be converted to bash id be able to uninstall php, could anyone assist?

<?php
function generateKey(String $password) 
{
$date = time();
$key = ( date( 'd', $date ) * 2 ) + ( date( 'm', $date) * 100 * 3 ) + ( date( 'y', $date ) * 10000 * 17 );
return md5( $key . $password );
}

print generateKey('abcd1234');

Upvotes: 0

Views: 641

Answers (2)

tshiono
tshiono

Reputation: 22087

Would you please try:

generatekey() {
    local password=$1
    local d=$(date +%d)
    local m=$(date +%m)
    local y=$(date +%y)
    local key=$(( ${d#0} * 2 + ${m#0} * 100 * 3 + ${y#0} * 10000 * 17 ))
    echo -n "${key}${password}" | md5sum | cut -d" " -f1
}

generatekey 'abcd1234'

Output:

f7e2b8ce423a63323f7b28271f052753
# As of Nov. 12, 2019

Hope this helps.

Upvotes: 1

richyen
richyen

Reputation: 10048

function generatekey {
  Y=`date +%y`
  M=`date +%m`
  D=`date +%d`
  key=$(( 2 * D + 300 * M + 170000 * Y ))
  echo -n $key$1 | md5sum
}

Output:

$ generatekey 'abcd1234'
f7e2b8ce423a63323f7b28271f052753

Upvotes: 0

Related Questions