joeljunstrom / go-luhn

Do What The F*ck You Want To Public License
19 stars 8 forks source link

How do I just generate a check-digit? #2

Open fawkesley opened 5 years ago

fawkesley commented 5 years ago

Hi @joeljunstrom !

Thanks for making this.

It seems your use case was about generating random numbers that validate with the Luhn algorithm.

How can I calculate a check digit for an existing number that's not random, e.g. one I've already made? I want to use Luhn for its intended purpose: issuing numbers to users, and detecting mistypes when they're passed back to me.

Thanks!

joeljunstrom commented 5 years ago

Hey buddy! Since I wrote it 6 years ago I haven't got a clue what my use case was but yeah looks like it =)

I guess the cleanest would be to make generateControlDigit (https://github.com/joeljunstrom/go-luhn/blob/master/luhn.go#L51-L59) public which should give you what you want?

You could probably use GenerateWithPrefix already if you pass your full number as prefix and the size of your number + 1. Haven't validated that it won't blow up but looks like it should result in what you need. Bit hacky tho =)

fawkesley commented 5 years ago

Gotcha, thanks! Yes exporting generateControlDigit would be great.

Are you open to contributions on this repo? After half an hour playing with it I've got some ideas about a useful external API.

Thanks again

joeljunstrom commented 5 years ago

@paulfurley go wild =)