Skip to content

Instantly share code, notes, and snippets.

@foxted
Last active August 30, 2015 22:39
Show Gist options
  • Save foxted/909aef1047d58960890a to your computer and use it in GitHub Desktop.
Save foxted/909aef1047d58960890a to your computer and use it in GitHub Desktop.
API Consumer ID & Consumer secret generator
<?php
function gen_oauth_creds() {
// Get a whole bunch of random characters from the OS
$fp = fopen('/dev/urandom','rb');
$entropy = fread($fp, 32);
fclose($fp);
// Takes our binary entropy, and concatenates a string which represents the current time to the microsecond
$entropy .= uniqid(mt_rand(), true);
// Hash the binary entropy
$hash = hash('sha512', $entropy);
// Base62 Encode the hash, resulting in an 86 or 85 character string
$hash = base64_encode($hash);
// Chop and send the first 80 characters back to the client
return array(
'consumer_key' => substr($hash, 0, 32),
'shared_secret' => substr($hash, 32, 48)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment