Powell.CouponCode
1.0.3
dotnet add package Powell.CouponCode --version 1.0.3
NuGet\Install-Package Powell.CouponCode -Version 1.0.3
<PackageReference Include="Powell.CouponCode" Version="1.0.3" />
paket add Powell.CouponCode --version 1.0.3
#r "nuget: Powell.CouponCode, 1.0.3"
// Install Powell.CouponCode as a Cake Addin #addin nuget:?package=Powell.CouponCode&version=1.0.3 // Install Powell.CouponCode as a Cake Tool #tool nuget:?package=Powell.CouponCode&version=1.0.3
csharp-algorithm-coupon-code
Implementation of Perl's Algorithm::CouponCode in C#
Supports net35, net40, net45 and netstandard20. netstandard not supported due to System.Security.Cryptography deficit.
Port of http://search.cpan.org/dist/Algorithm-CouponCode/
Copyright 2011 Grant McLean grantm@cpan.org
With nods to the various other language ports for guidance:
- https://github.com/baxang/coupon-code/blob/master/lib/coupon_code.rb
- https://github.com/chilts/node-coupon-code/blob/master/coupon-code.js
Synopsis
A 'Coupon Code' is made up of letters and numbers grouped into 4 character 'parts'. For example, a 3-part code might look like this:
1K7Q-CTFM-LMTC
Coupon Codes are random codes which are easy for the recipient to type accurately into a web form. An example application might be to print a code on a letter to a customer who would then enter the code as part of the registration process for web access to their account.
Features of the codes that make them well suited to manual transcription:
- The codes are not case sensitive.
- Not all letters and numbers are used, so if a person enters the letter 'O' we can automatically correct it to the digit '0' (similarly for I ⇒ 1, S ⇒ 5, Z ⇒ 2).
- The 4th character of each part is a checkdigit, so client-side scripting can be used to highlight parts which have been mis-typed, before the code is even submitted to the application's back-end validation.
- The checkdigit algorithm takes into account the position of the part being keyed. So for example '1K7Q' might be valid in the first part but not in the second so if a user typed the parts in the wrong boxes then their error could be highlighted.
- The code generation algorithm avoids 'undesirable' codes. For example any code in which transposed characters happen to result in a valid checkdigit will be skipped. Any generated part which happens to spell an 'inappropriate' 4-letter word (e.g.: 'P00P') will also be skipped.
- The code returned by Generate() is random, but not necessarily unique. If your application requires unique codes, it is your responsibility to avoid duplicates (for example by using a unique index on your database column).
- The codes are generated using a SHA1 cryptographic hash of a plaintext. If you
do not supply a plaintext, one will be generated for you (using RNGCryptoServiceProvider).
In the event that an 'inappropriate' code is created, the generated hash will be used as a plaintext input for generating a new hash and the process will be repeated. - Each 4-character part encodes 15 bits of random data, so a 3-part code will incorporate 45 bits making a total of 2^45 (approximately 35 trillion) unique codes.
string Generate(Options opts) method:
Returns a coupon code as a string of 4-character parts separated by '-' characters. The following optional named parameters may be supplied:
Example:
var opts = new Options();
var ccb = new CouponCodeBuilder();
var badWords = ccb.BadWordsList;
var code = ccb.Generate(opts);
// code example EED9-128L-BJUQ
Parts
The number of parts desired. Must be a number in the range 1 - 6. Default is 3.
Plaintext
A byte string which will be hashed using Digest::SHA to produce the code. If you do not supply your own plaintext then a random one will be generated for you.
string Validate(string code, Options opts)
Takes a code, cleans it up and validates the checkdigits. Returns the normalised (and untainted) version of the code on success or undef on error. The following named parameters may be supplied:
Code
The code to be validated. The parameter is mandatory.
Parts
The number of parts you expect the code to contain. Default is 3.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net35 is compatible. net40 is compatible. net403 was computed. net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Powell.CouponCode:
Repository | Stars |
---|---|
episerver/Foundation
Foundation offers a starting point that is intuitive, well-structured and modular allowing developers to explore CMS, Commerce, Personalization, Search and Navigation, Data Platform and Experimentation.
|