-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'UTSAVS26:main' into main
- Loading branch information
Showing
23 changed files
with
27,482 additions
and
6 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# URL Shortener | ||
|
||
## 🚩 Aim | ||
To create a simple URL shortener that converts long URLs into shorter, more manageable versions. | ||
|
||
## 📝 Description | ||
This is a basic URL shortener implemented in Python. The script takes a long URL as input and generates a short, random URL that can be used to easily reference the original URL. This project serves as an excellent practice for string manipulation, data storage techniques, and can be extended to include a web interface. | ||
|
||
### Features: | ||
- Generates a unique shortened URL for each long URL provided. | ||
- Retrieves the original URL when provided with a shortened version. | ||
- Easily expandable into a web application using frameworks like Flask or Django. | ||
|
||
## ⚙️ How It Works | ||
1. **URL shortening**: The script generates a random 6-character string using uppercase, lowercase letters, and digits. This string is appended to a base URL (`http://short.url/`) to create the shortened URL. | ||
2. **Storage**: A Python dictionary is used to store the mapping between long URLs and their shortened versions. | ||
3. **URL retrieval**: The original long URL can be retrieved by inputting the shortened URL. | ||
|
||
## 🚀 Getting Started | ||
|
||
### Prerequisites: | ||
- Python 3.x installed on your machine. | ||
|
||
### Installation: | ||
|
||
1. Clone this repository: | ||
```bash | ||
git clone https://github.com/your-username/url-shortener.git | ||
cd url-shortener | ||
|
||
2. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import string | ||
import random | ||
|
||
class URLShortener: | ||
def __init__(self): | ||
self.url_mapping = {} # Stores long URLs and their corresponding short URLs | ||
self.base_url = "http://short.url/" | ||
self.chars = string.ascii_letters + string.digits # Characters for generating short URL codes | ||
|
||
def generate_short_code(self, length=6): | ||
"""Generate a random short code for the URL.""" | ||
return ''.join(random.choice(self.chars) for _ in range(length)) | ||
|
||
def shorten_url(self, long_url): | ||
"""Shorten a given long URL.""" | ||
if long_url in self.url_mapping: | ||
return self.url_mapping[long_url] | ||
else: | ||
short_code = self.generate_short_code() | ||
short_url = self.base_url + short_code | ||
self.url_mapping[long_url] = short_url | ||
return short_url | ||
|
||
def retrieve_url(self, short_url): | ||
"""Retrieve the original long URL from the shortened version.""" | ||
for long_url, short in self.url_mapping.items(): | ||
if short == short_url: | ||
return long_url | ||
return None | ||
|
||
# Driver code | ||
if __name__ == "__main__": | ||
shortener = URLShortener() | ||
|
||
# Example Usage | ||
print("=== URL Shortener ===") | ||
long_url = input("Enter the URL to shorten: ") | ||
short_url = shortener.shorten_url(long_url) | ||
print(f"Shortened URL: {short_url}") | ||
|
||
# Retrieve original URL | ||
print("\n=== Retrieve Original URL ===") | ||
original_url = shortener.retrieve_url(short_url) | ||
print(f"Original URL for {short_url}: {original_url}") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import random | ||
import numpy | ||
from PIL import Image | ||
|
||
def derandomize(img, seed, original_shape): | ||
random.seed(seed) | ||
|
||
# Recreate the shuffled indices for both x and y | ||
new_y = list(range(original_shape[0])) | ||
new_x = list(range(original_shape[1])) | ||
random.shuffle(new_y) | ||
random.shuffle(new_x) | ||
|
||
# Create an empty array to store the reconstructed image | ||
reconstructed = numpy.empty_like(img) | ||
|
||
# Reverse the shuffling by mapping randomized positions back to the original positions | ||
for i, y in enumerate(new_y): | ||
for j, x in enumerate(new_x): | ||
reconstructed[y][x] = img[i][j] | ||
|
||
return numpy.array(reconstructed) | ||
|
||
if __name__ == "__main__": | ||
# Open the scrambled (encrypted) image | ||
scrambled_img = Image.open("encrypted.png") | ||
scrambled_array = numpy.array(scrambled_img) | ||
|
||
original_shape = scrambled_array.shape | ||
|
||
# Define a range of potential seed values (assuming timestamp or a fixed range) | ||
start_seed = int(input("Enter the starting seed to brute-force: ")) | ||
end_seed = int(input("Enter the ending seed to brute-force: ")) | ||
|
||
for seed in range(start_seed, end_seed): | ||
reconstructed_img = derandomize(scrambled_array, seed, original_shape) | ||
|
||
# Save the reconstructed image | ||
image = Image.fromarray(reconstructed_img) | ||
image.save(f"decrypted_{seed}.png") | ||
|
||
# Optionally, display the reconstructed image to see if it's correct | ||
image.show() | ||
print(f"Tried seed: {seed}") | ||
|
Oops, something went wrong.