ccgen

package module
v0.0.0-...-2c486f8 Latest Latest
Warning

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

Go to latest
Published: May 8, 2019 License: MIT Imports: 3 Imported by: 0

README

ccgen

Minimalistic Go package for a random Luhn-compliant credit card numbers generation

Go Report Card Build Status

Introduction

The package allows generating random credit card numbers according to the Luhn algorithm. Card prefix (first N digits of a card number) will correspond to the selected card scheme.

Usage

Install the package with:

go get github.com/nsuprun/ccgen

Import:

import (
	"github.com/nsuprun/ccgen"
)

At the moment the following card schemes are supported:

const (
	AmericanExpress CardType = iota + 1
	DinersClub
	DinersClubUS
	Discover
	JCB
	Laser
	Maestro
	Mastercard
	Solo
	Unionpay
	Visa
	Mir
)
func main() {
	// To generate a card number of a random card type
	fmt.Printf("Random card type number: %s\n", ccgen.Generate())

	// To generate a card number of a choosen type and random valid length
	fmt.Printf("American Express: %s\n", ccgen.AmericanExpress.Generate())
	fmt.Printf("Diners Club: %s\n", ccgen.DinersClub.Generate())

	// To generate a card number of selected valid length:
	// Solo 19 digits card
	fmt.Printf("Solo: %s\n", ccgen.Solo.GenerateOfLength(19))

	// To validate if card number of given type is valid
	fmt.Printf("Card number is valid: %t\n", ccgen.DinersClub.ValidNumber("36690592674457"))
}

Output:

Random card number: 6767575111052941
American Express: 346905926744572
Diners Club: 36690592674457
Solo: 6334690592674457710
Card number is wvalid: true

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate() string

Generate generates card number of a random card type

Types

type CardType

type CardType int

CardType represents card number type to be generated

const (
	AmericanExpress CardType = iota + 1
	DinersClub
	DinersClubUS
	Discover
	JCB
	Laser
	Maestro
	Mastercard
	Solo
	Unionpay
	Visa
	Mir
)

Supported card types

func (CardType) Generate

func (t CardType) Generate() string

Generate generates random card number of CardType

func (CardType) GenerateOfLength

func (t CardType) GenerateOfLength(l int) string

GenerateOfLength generates card number of specified length If length is not valid, random among valid ones will be used

func (CardType) ValidLength

func (t CardType) ValidLength(l int) bool

ValidLength validates if specified card length is valid for the card type

func (CardType) ValidNumber

func (t CardType) ValidNumber(cnumber string) bool

ValidNumber validates card number for a prefix and Luhn algo @todo: add card prefix check

Jump to

Keyboard shortcuts

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