happ

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2025 License: GPL-3.0 Imports: 9 Imported by: 0

README

Encryption/Decryption Module

A Go module for encrypting and decrypting Happ links using RSA encryption with PKCS1v15 padding.

Features

  • 🔐 RSA encryption/decryption with PKCS1v15 padding
  • 🔗 All Happ link format support (happ://crypt/..., happ://crypt2/..., happ://crypt3/...)
  • 🗝️ Multiple key version support
  • 🛡️ Comprehensive error handling

Installation

go get github.com/nf776/[email protected]
go install github.com/ckeiituk/[email protected]

Usage

package main

import (
    "fmt"
    "log"
    "github.com/nf776/happ-decryptor"
)

func main() {
    // Initialize keys
    privateKeys := map[string]string {
        "crypt":  "keys/private_crypt.pem",
        "crypt2": "keys/private_crypt2.pem",
        "crypt3": "keys/private_crypt3.pem",
    }

    publicKeys := map[string]string {
        "crypt":  "keys/public_crypt.pem",
        "crypt2": "keys/public_crypt2.pem",
        "crypt3": "keys/public_crypt3.pem",
    }

    // Initialize processor with key paths
    processor, err := happ.New(privateKeys, publicKeys)
    if err != nil {
        log.Fatal(err)
    }

    // Encrypt data
    result, err := processor.Encrypt("secret data", "crypt3")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Encrypted link: %s\n", result.Link)

    // Decrypt data
    decrypted, err := processor.Decrypt(result.Link)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Decrypted data: %s\n", decrypted.DecryptedData)
    fmt.Printf("Used key: %s\n", decrypted.UsedKey)
}

Key Requirements

  • Private Keys: For decryption (keys from app)
  • Public Keys: For encryption (PKCS8 PEM format)
  • Key Versions: Support for crypt, crypt2, crypt3 versions

Error Handling

The module provides comprehensive error handling with clear error messages:

result, err := processor.Encrypt("data", "unknown")
if err != nil {
    switch err.Error() {
    case happ.ErrEmptyPath:
        // Handle missing path
    case happ.ErrPrivateKeyNotRSA:
        // Handle private key error
    // ... other cases
    }
}

Smart Decryption

  • Automatically tries multiple key versions if the specified one fails
  • Fallback order: specified version → crypt → crypt2 → crypt3
  • Returns the actual key used for decryption

Documentation

Index

Constants

View Source
const (
	ErrEmptyPath            = "path cannot be empty"
	ErrFileRead             = "failed to read file"
	ErrFileEmpty            = "file is empty"
	ErrPEMDecode            = "failed to decode PEM"
	ErrPEMNil               = "PEM block is nil"
	ErrPrivateKeyNotRSA     = "private key is not an RSA private key"
	ErrPublicKeyNotRSA      = "public key is not an RSA private key"
	ErrPrivateKeyParse      = "private key parse error"
	ErrPrivateKeyNil        = "private key is nil"
	ErrPublicKeyParse       = "public key parse error"
	ErrPublicKeyNil         = "public key is nil"
	ErrEmptyData            = "data is empty"
	ErrEmptyVersion         = "version cannot be empty"
	ErrDecryptEmptyLink     = "empty link"
	ErrDecryptBadData       = "none of the keys could decrypt the data"
	ErrWrongVersion         = "public key for version %s not found"
	ErrWrongKeyForVersion   = "public key for version %s is invalid (nil)"
	ErrInvalidLinkFormat    = "invalid link format"
	ErrEncryptedDataIsEmpty = "encrypted data is empty"
	ErrB64Decode            = "base64 decode fails: %v"
	ErrRSADecode            = "RSA decode fails: %v"
	ErrLargeData            = "data too large"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Processor

type Processor struct {
	// contains filtered or unexported fields
}

func New

func New(privateKeyPaths map[string]string, publicKeyPaths map[string]string) (*Processor, error)

func (*Processor) Decrypt

func (p *Processor) Decrypt(link string) (Result, error)

func (*Processor) Encrypt

func (p *Processor) Encrypt(data, version string) (Result, error)

type Result

type Result struct {
	Version       string
	UsedKey       string
	EncryptedData string
	DecryptedData string
	Link          string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL