Last active
April 1, 2019 16:32
Star
You must be signed in to star a gist
HMAC Generator based on path(URL), expiry time and reuse times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hmac | |
import org.apache.commons.codec.binary.Base64 | |
import java.net.URLEncoder | |
import java.time.LocalDateTime | |
import java.time.OffsetDateTime | |
import javax.crypto.Mac | |
import javax.crypto.spec.SecretKeySpec | |
import java.util.HashMap | |
const val PATH_URI = "http://your.url.com" | |
const val SHARED_SECRET = "Your_Key" | |
const val REUSE = 0 | |
const val SHA256 = "HmacSHA256" | |
fun main() { | |
val requestParams = HashMap<String, String>() | |
requestParams["pathURI"] = PATH_URI | |
requestParams["expiry"] = expiryTime(10) | |
requestParams["reuse"] = REUSE.toString() | |
val encodedQueryParams = requestParams.keys.stream() | |
.map { key -> "${encodeValue(key)}=${encodeValue(requestParams[key].orEmpty())}" } | |
.reduce { p1: String?, p2: String? -> "$p1&$p2" } | |
.map { s -> "?$s" } | |
.orElse("") | |
println(encodedQueryParams) | |
val sha256HMAC = Mac.getInstance(SHA256) | |
val secretKey = SecretKeySpec(SHARED_SECRET.toByteArray(), SHA256) | |
sha256HMAC.init(secretKey) | |
val hash = sha256HMAC.doFinal(encodedQueryParams.toByteArray()) | |
val hexHash = bytesToHexString(hash) | |
println(hexHash) | |
val digest = "$encodedQueryParams,$hexHash" | |
val base64 = Base64.encodeBase64(digest.toByteArray()) | |
val token = "authtoken=${base64.toString(Charsets.UTF_8)}" | |
println(token) | |
} | |
fun encodeValue(value: String): String { | |
return URLEncoder.encode(value, Charsets.UTF_8.toString()) | |
} | |
fun bytesToHexString(bytes: ByteArray): String { | |
var st = "" | |
for (b in bytes) { | |
st += String.format("%02x", b) | |
} | |
return st | |
} | |
fun expiryTime(hours: Long): String { | |
val current = LocalDateTime.now() | |
val future = current.plusHours(hours) | |
return future.toEpochSecond(OffsetDateTime.now().offset).toString() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import hmac | |
import hashlib | |
import time | |
from datetime import datetime, timedelta | |
from urllib.parse import urlencode | |
def create_vxttoken_data(path_uri, expiry, reuse, shared_secret): | |
# Combine the parameters into a URL-encoded string | |
message = [] | |
message.extend([('pathURI', path_uri)]) | |
message.extend([('expiry', expiry)]) | |
message.extend([('reuse', reuse)]) | |
print(message) | |
url_encoded_message = urlencode(message) | |
hm = hmac.new(shared_secret.encode('utf-8'), url_encoded_message.encode('utf-8'), hashlib.sha256) | |
digest = url_encoded_message + ',' + hm.hexdigest() | |
b64_encoded = base64.b64encode(digest.encode('utf-8')) | |
return 'vxttoken=%s' % b64_encoded | |
if __name__ == '__main__': | |
# Configure the parameters required for the token | |
pathURI = 'http://your.url.com/*' | |
# Your shared secret | |
shared_secret = 'Your_Key' | |
# Generate an expiry time 10 hours in the future | |
future_time = datetime.now() + timedelta(hours=10) | |
expiry = int(time.mktime(future_time.timetuple())) | |
reuse = 0 | |
vxtToken = create_vxttoken_data(pathURI, expiry, reuse, shared_secret) | |
print(vxtToken) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment