-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsms_module.py
More file actions
70 lines (53 loc) · 2.12 KB
/
sms_module.py
File metadata and controls
70 lines (53 loc) · 2.12 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
60
61
62
63
64
65
66
67
68
69
70
import base64
import hmac
import hashlib
import time
from typing import Dict, Optional
class SMSGateway:
"""Stub SMS gateway interface."""
def send_sms(self, to: str, message: str) -> bool:
print(f"Simulating SMS send to {to}: {message}")
return True
def receive_sms(self, from_num: str, message: str) -> Dict[str, str]:
return {"from": from_num, "body": message}
gateway = SMSGateway()
SECRET_KEY = b"secret_key"
def encode_token(token: str) -> str:
timestamp = str(int(time.time()))
token_hash = hashlib.sha256(token.encode()).hexdigest()[:16]
data = f"{timestamp}:{token}:{token_hash}"
hmac_sig = hmac.new(SECRET_KEY, data.encode(), hashlib.sha256).hexdigest()[:32]
encoded = base64.b32encode(f"{data}:{hmac_sig}".encode()).decode()
return encoded[:140]
def send_sms_token(to: str, token: str) -> bool:
encoded = encode_token(token)
return gateway.send_sms(to, encoded)
def parse_and_validate_sms(message: str) -> Optional[str]:
try:
decoded = base64.b32decode(message.encode()).decode()
timestamp, token, token_hash, received_hmac = decoded.split(":")
data = f"{timestamp}:{token}:{token_hash}"
expected_hmac = hmac.new(SECRET_KEY, data.encode(), hashlib.sha256).hexdigest()[:32]
if received_hmac != expected_hmac:
return None
if hashlib.sha256(token.encode()).hexdigest()[:16] != token_hash:
return None
if int(time.time()) - int(timestamp) > 3600:
return None
print(f"Token reconstructed and validated: {token}")
return token
except Exception:
return None
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print("Usage: sms_module.py send <phone> <token> | receive <message>")
raise SystemExit(1)
if sys.argv[1] == "send" and len(sys.argv) >= 4:
send_sms_token(sys.argv[2], sys.argv[3])
elif sys.argv[1] == "receive" and len(sys.argv) >= 3:
token = parse_and_validate_sms(sys.argv[2])
print(f"Validated token: {token}")
else:
print("Invalid arguments")
raise SystemExit(1)