From d25b3e1a2bc43158b9ef973c997f8c3c6708ba51 Mon Sep 17 00:00:00 2001 From: Pablo Diehl Date: Mon, 22 Jan 2024 15:40:15 -0300 Subject: [PATCH 1/3] feat: Update PHP script to PHP8 --- secure_token.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/secure_token.php b/secure_token.php index 8a3418e..de8db7a 100755 --- a/secure_token.php +++ b/secure_token.php @@ -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"; ?> From 42687604f49bfd1d03ab7c3c7943f549709a5868 Mon Sep 17 00:00:00 2001 From: Pablo Diehl Date: Mon, 22 Jan 2024 15:41:28 -0300 Subject: [PATCH 2/3] feat: Update Python script to Python 3 --- secure_token.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/secure_token.py b/secure_token.py index 0c8bd87..70fc6e3 100755 --- a/secure_token.py +++ b/secure_token.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import base64 import hashlib @@ -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) From a123f009d976a307f4da7021e6e6a2b181c05c70 Mon Sep 17 00:00:00 2001 From: Pablo Diehl Date: Mon, 22 Jan 2024 15:42:16 -0300 Subject: [PATCH 3/3] feat: Add node js sample --- secure_token.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 secure_token.js diff --git a/secure_token.js b/secure_token.js new file mode 100644 index 0000000..32abccd --- /dev/null +++ b/secure_token.js @@ -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}`); +