Socket
Socket
Sign inDemoInstall

@swapnil-pando/url-shortening

Package Overview
Dependencies
2
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @swapnil-pando/url-shortening

A URL shortener using SHA1 encryption with no external api


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Install size
151 kB
Created
Weekly downloads
 

Readme

Source

Introduction

This is an implementation of a URL Shortener Implementation without use of any external API using SHA1 encryption.

The long URL which will be given to us will be in the format of
https://localhost:5050/track-link/A-23-Depot-Z-M-01?id=${uuid}"

uuid is unique identifier of 36 characters

Problem Statement

The problem statement is to convert this long url into a shortened url. Whenever we click on this shortened url , it should redirect us to the long url and eventually to the webpage that the long url is directing to.

Back of the Envelope Estimation

We want our shortened url to be of length ≤ 7.
Character set of our shortened url includes [A-Z], [a-z], [0–9] which is 26 + 26 + 10 = 62 characters.
So with this we can have 62 ^ 7 = 35 billion unique urls. And this is the reason why we will be using base-62 encryption as well in our implementation.

Implementation

The original url is encrypted using SHA-1 to a 40 hex encrypted hash.

We slice this 40 hex hash into 5 equal parts of 8 hex each, which is converted into decimal which again is base62 encoded.

This splitting into 5 equal parts is done to make sure that our final base-62 encrypted string will have a length of ≤ 7, since the main target is to produce a shortened url.

This shortened url is then stored in database along with the original long url as a kind of a mapper, which will be used while redirection of the shortened url to the original url.

Now while storing the shortened url in database, we will first check if this shortened url exists or not. If it exists, then we will put some degree of randomness and will generate the shortened url again.

This retry count has been limited to 10 in our implementation, so as to not affect latency of the system a lot. Also as per our functional requirement, the expiry of the shortened url in database is 3 months.

Test Results

One can run test/url-shorten.js file to see the working of shortener

Repeats in a run of 1000000 are 99 with repeat percentage of 0.009899999999999999.

Repeats in a run of 8500000 are 8416 with repeat percentage of 0.09901176470588235.

Repeats in a run of 10000000 are 11725 with repeat percentage of 0.11725.
https://swapnilsatpathycse24.medium.com/url-shortener-b5b231068e0

Keywords

FAQs

Last updated on 27 Sep 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc