-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path51_shortURL.py
More file actions
30 lines (23 loc) · 782 Bytes
/
51_shortURL.py
File metadata and controls
30 lines (23 loc) · 782 Bytes
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
import hashlib
class UrlShortner:
def __init__(self):
self.short_to_url = dict()
self.algo = hashlib.sha256
def shorten(self, url):
sha_signature = self.algo(url.encode()).hexdigest()
# print(url.encode())
print(sha_signature)
short_hash = sha_signature[:6]
print(short_hash)
self.short_to_url[short_hash] = url
return short_hash
def restore(self, short_hash):
return self.short_to_url[short_hash]
url = "https://teams.microsoft.com/_#/calendarv2"
US = UrlShortner()
short = US.shorten(url)
assert US.restore(short) == url
# Functions associated :
# encode() : Converts the string into bytes to be acceptable by hash function.
# hexdigest() : Returns the encoded data in hexadecimal format.
# Sha = Secure hash algorithms