-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreatePricePointID.py
More file actions
59 lines (45 loc) · 1.72 KB
/
Copy pathcreatePricePointID.py
File metadata and controls
59 lines (45 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
import json
import base64
import argparse
def base64url_encode(data: bytes) -> str:
return base64.urlsafe_b64encode(data).rstrip(b'=').decode('utf-8')
def base64url_decode(data: str) -> bytes:
padding = '=' * (-len(data) % 4)
return base64.urlsafe_b64decode(data + padding)
def encode_json_to_base64(s, t, p):
"""
Create a JSON object with the given parameters and encode it with base64url.
Base64URL is a URL-safe variant that replaces '+' with '-' and '/' with '_'.
Args:
s (str): The 's' parameter
t (str): The 't' parameter
p (str): The 'p' parameter
Returns:
str: Base64URL encoded JSON string
"""
# Create the JSON object
json_data = {"s":s,"t":t,"p":p}
# Convert to JSON string
json_string = json.dumps(json_data)
# Remove any whitespace from the JSON string
json_string = json_string.replace(" ", "")
# Encode with base64url
encoded = base64url_encode(json_string.encode('utf-8'))
return encoded
def main():
# Set up argument parser
parser = argparse.ArgumentParser(description='Encode JSON with base64url')
parser.add_argument('--s', required=True, help='Value for the "s" parameter')
parser.add_argument('--t', required=True, help='Value for the "t" parameter')
parser.add_argument('--p', required=True, help='Value for the "p" parameter')
# Parse arguments
args = parser.parse_args()
# Encode JSON with base64url
encoded = encode_json_to_base64(args.s, args.t, args.p)
# Also print the decoded result to verify
decoded = base64url_decode(encoded)
print(f"{decoded}")
print(f"{encoded}")
if __name__ == "__main__":
main()