Skip to content

ayush-jadaun/ShortyQ

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ShortyQ

A secure URL shortener package that uses quantum-inspired encryption techniques to generate and manage short URLs. This package provides a robust and secure way to create shortened URLs while maintaining privacy and security.

Features

  • πŸ”’ Quantum-inspired encryption for enhanced security
  • βš™οΈ Fully configurable URL length and salt rounds
  • πŸ”„ Multiple rounds of encryption using different algorithms
  • πŸ’Ύ Flexible storage integration (PostgreSQL, MongoDB, Redis, etc.)
  • πŸ“ TypeScript support with full type definitions
  • βœ… Comprehensive test coverage (98%+)
  • 🚨 Robust error handling for invalid URLs
  • ⚑ High performance with concurrent operation support

Installation

npm install shortyq

Quick Start

Basic Usage

import { ShortyQ } from "shortyq";

// Initialize with default options
const shortyQ = new ShortyQ();

// Create a short URL with encryption
const { shortCode, encryptedData } = shortyQ.createShortUrl(
  "https://example.com/long/path"
);
console.log(`Short Code: ${shortCode}`); // e.g., "Ax7Yt9"

// Store both shortCode and encryptedData in your database
await db.save({ shortCode, encryptedData });

// Later, retrieve and decrypt the URL
const storedData = await db.get(shortCode);
if (storedData) {
  const originalUrl = shortyQ.decryptUrl(storedData.encryptedData);
  console.log(`Original URL: ${originalUrl}`);
}

Visit the examples folder for database integration examples with PostgreSQL, MongoDB, and Redis.

Advanced Configuration

const shortyQ = new ShortyQ({
  // Length of generated short codes (4-100 chars, default: 8)
  urlLength: 8,

  // PBKDF2 iterations for key derivation (default: 10)
  // Higher values = more secure but slower
  saltRounds: 12,

  // Seed for quantum noise generation (default: random)
  // Use fixed value for reproducible results
  quantumSeed: 42,
});

Configuration Options

Option Type Default Range Description
urlLength number 8 4-100 Length of generated short codes
saltRounds number 10 1-∞ PBKDF2 iterations for key derivation
quantumSeed number random any Seed for quantum noise generation

Security Best Practices

  1. Salt Rounds

    • Default: 10 rounds
    • Recommended: 12-15 rounds for production
    • Higher values increase security but impact performance
    • Example: saltRounds: 15 for high-security applications
  2. URL Length

    • Minimum: 4 characters
    • Maximum: 100 characters
    • Recommended: 8-12 characters for good balance
    • Example: urlLength: 10 for production use
  3. Quantum Seed

    • Default: Random value
    • Use fixed value for reproducible results
    • Example: quantumSeed: process.env.QUANTUM_SEED

Error Handling

try {
  // Validate URL format
  const { shortCode, encryptedData } = shortyQ.createShortUrl("invalid-url");
} catch (error) {
  if (error.message === "Invalid URL format") {
    console.error("Please provide a valid URL");
  } else if (error.message.includes("URL length")) {
    console.error("URL length is out of bounds");
  } else {
    console.error("Error:", error.message);
  }
}

// Handle decryption failures gracefully
const originalUrl = shortyQ.decryptUrl(invalidData);
if (originalUrl === null) {
  console.error("Failed to decrypt URL - data may be corrupted");
}

Supported URL Types

// Basic URLs
shortyQ.createShortUrl("https://example.com");

// URLs with query parameters
shortyQ.createShortUrl("https://api.example.com/search?q=test&sort=desc");

// URLs with special characters
shortyQ.createShortUrl(
  "https://example.com/path/with/special/chars/!@#$%^&*()"
);

// URLs with Unicode characters
shortyQ.createShortUrl("https://example.com/unicode/path/πŸš€/ζ΅‹θ―•/тСст");

// URLs with fragments
shortyQ.createShortUrl("https://example.com/page#section1");

TypeScript Support

import { ShortyQ, EncryptedData, ShortyQOptions } from "shortyq";

// Type-safe options
const options: ShortyQOptions = {
  urlLength: 8,
  saltRounds: 12,
  quantumSeed: 42,
};

// Type-safe encrypted data
interface URLRecord {
  shortCode: string;
  encryptedData: EncryptedData;
  createdAt: Date;
  expiresAt?: Date;
}

// Store URL data with proper typing
const record: URLRecord = {
  shortCode,
  encryptedData,
  createdAt: new Date(),
  expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 hours
};

Security Features

ShortyQ implements several layers of security:

  1. Quantum-inspired noise generation

    • Uses seeded pseudo-random number generation
    • Generates 32 bytes of quantum-inspired noise
    • Applies trigonometric transformations for enhanced randomness
    • Includes timestamp for unique encryption even with same seed
  2. Multiple rounds of encryption

    • First layer: AES encryption using quantum noise as key
    • Second layer: AES with PBKDF2-derived key
    • Final layer: AES with SHA3-combined keys
    • Each layer adds additional security
  3. URL Security

    • Multiple layers of encryption for enhanced security
    • Unique encryption for each URL (same URL gets different encrypted data)
    • Collision detection and prevention
    • Input validation and sanitization
    • Maximum URL length limit (4096 characters)
    • Secure key derivation and management
  4. Storage Security

    • No built-in storage (use your preferred database)
    • Encrypted data structure
    • Optional expiration support
    • Type-safe interfaces

Performance

Benchmark Results

Operation Average 95th Percentile 99th Percentile
Single URL Encryption 463.4ms 463.4ms 463.4ms
Complex URL Encryption 441.34ms 441.34ms 441.34ms
URL Decryption 457.13ms 457.13ms 457.13ms
Concurrent Operations 45.62ms 45.62ms 45.62ms

Performance Characteristics

  • Consistent Performance: All operations show stable execution times with minimal variance
  • Concurrent Optimization: Parallel operations are ~10x faster than single operations
  • Complex URL Handling: Slightly faster than simple URLs due to optimized processing
  • Memory Efficiency: No persistent storage overhead
  • Scalability: Excellent concurrent operation support

Performance Optimization Tips

  1. Batch Processing

    • Use concurrent operations for multiple URLs
    • Process URLs in batches of 10-20 for optimal performance
  2. Caching Strategy

    • Cache frequently accessed short codes
    • Implement Redis or in-memory cache for hot paths
  3. Database Optimization

    • Use indexed columns for short codes
    • Implement connection pooling
    • Consider read replicas for high-traffic scenarios
  4. Resource Management

    • Monitor memory usage for large URL batches
    • Implement rate limiting for API endpoints
    • Use connection pooling for database operations

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published