0

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');
1
  • If you have php on the linux system you can run the php right in the terminal. Just type php filename.php but you'd have to replace the "print" with "echo" Commented Nov 12, 2019 at 8:00

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

this is perfect, now i no longer require php on my tiny cloud VPS! Thank you!
@user2879090 I have had a mistake in my answer. The command fails on specific days which contains "08" or "09" field such as 8th day, 9th day, or Aug., Sep. due to the leading zero. Would you please update your copy with my last answer? Sorry for the inconvenience. BR.
0
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

1 Comment

You don't want that . in there. Or the newline that plain echo inserts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.