Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update samples #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions secure_token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const crypto = require('crypto');

const secret = 'mysecret';
const uri = '/my/uri';
const expire = '1470055000';

const md5 = crypto.createHash('md5');
md5.update((secret + uri + expire), 'utf-8');
const token = Buffer.from(md5.digest()).toString('base64').replace(/=|\+|\//g, (match) => ({
'=': '',
'+': '-',
'/': '_',
}[match]));

console.log(`http://www.example.org${uri}?st=${token}&e=${expire}`);

6 changes: 2 additions & 4 deletions secure_token.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
$uri = "/my/uri";
$expire = "1470055000";

$token = base64_encode(md5($secret . $uri . $expire, true));
$token = str_replace("=", "", $token);
$token = strtr($token, "+/", "-_");
$token = str_replace(["=", '+', '/'], ['', '-', '_'], base64_encode(md5($secret . $uri . $expire, true)));

print "http://www.example.org" . $uri . "?st=" . $token . "&e=" . $expire . "\n";
echo "http://www.example.org" . $uri . "?st=" . $token . "&e=" . $expire . "\n";

?>
9 changes: 5 additions & 4 deletions secure_token.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

import base64
import hashlib
Expand All @@ -8,7 +8,8 @@
expire = '1470055000'

md5 = hashlib.md5()
md5.update(secret + uri + expire)
token = base64.b64encode(md5.digest()).replace('=','').replace('+','-').replace('/','_')
md5.update((secret + uri + expire).encode('utf-8'))
token = base64.b64encode(md5.digest()).decode('utf-8').replace('=','').replace('+','-').replace('/','_')

print('http://www.example.org%s?st=%s&e=%s' % (uri, token, expire))

print 'http://www.example.org%s?st=%s&e=%s' % (uri, token, expire)
Loading