EOS-WebAPI/Common/UrlShortener.cs
Nidhi Bhargava d0ac8a7790 Code Commit
2025-09-04 17:30:22 +05:30

35 lines
1.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
namespace VECV_WebApi.Common
{
public class UrlShortener
{
public string ShortenUrl(string originalUrl)
{
// Compute MD5 hash of the original URL
using (MD5 md5 = MD5.Create())
{
byte[] inputBytes = Encoding.ASCII.GetBytes(originalUrl);
byte[] hashBytes = md5.ComputeHash(inputBytes);
// Convert the byte array to a hexadecimal string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
// Take the first 8 characters of the hash as the short URL
string shortenedHash = sb.ToString().Substring(0, 8);
// Append the hash to the base URL
return shortenedHash;
}
}
}
}