Skip to content

Commit

Permalink
Add normal token encoder feature (#2)
Browse files Browse the repository at this point in the history
* implement token encoder, include unit tests

* refactor: read test token from file

* fix: resolve failing test

* update README
  • Loading branch information
beesaferoot authored Aug 23, 2024
1 parent 2dd212b commit f7e54dd
Show file tree
Hide file tree
Showing 7 changed files with 559 additions and 39 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ Not all libraries have implemented the full range of features yet.

The **OpenPAYGO Go library** supports the following features:

| Feature | Status |
| ----------------- | -------------------- |
| OpenPAYGO Token | ✅ (beta) |
| OpenPAYGO Metrics | ❌ (not implemented) |
| Feature | Status |
| -------------------------- | -------------------- |
| OpenPAYGO Token (normal) ||
| OPenPAYGO Token (extended) | ❌ (not implemented) |
| OpenPAYGO Metrics | ❌ (not implemented) |

## Support

Expand Down
18 changes: 9 additions & 9 deletions token/extended/extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import (
)

const (
maxBase = 999999
maxActivationValue = 999999
tokenValueOffset = 1000000
MaxBase = 999999
MaxActivationValue = 999999
TokenValueOffset = 1000000
)

type tokenType uint8
type TokenType uint8

func getTokenBase(code uint64) uint64 {

Check failure on line 20 in token/extended/extended.go

View workflow job for this annotation

GitHub Actions / Lint golangci-lint

func `getTokenBase` is unused (unused)
return code % tokenValueOffset
return code % TokenValueOffset
}

func putBaseInToken(token, tokenbase uint64) (uint64, error) {

Check failure on line 24 in token/extended/extended.go

View workflow job for this annotation

GitHub Actions / Lint golangci-lint

func `putBaseInToken` is unused (unused)
if tokenbase > maxBase {
if tokenbase > MaxBase {
return 0, fmt.Errorf("invalid value")
}

Expand All @@ -32,7 +32,7 @@ func putBaseInToken(token, tokenbase uint64) (uint64, error) {
func generateNextToken(lastCode uint64, key []byte) uint32 {

Check failure on line 32 in token/extended/extended.go

View workflow job for this annotation

GitHub Actions / Lint golangci-lint

func `generateNextToken` is unused (unused)
conformedToken := make([]byte, 8)

binary.LittleEndian.PutUint64(conformedToken, lastCode)
binary.BigEndian.PutUint64(conformedToken, lastCode)

return convertHashToToken(generateHash(key, conformedToken))
}
Expand Down Expand Up @@ -106,11 +106,11 @@ func convertFrom4DigitsToken(digits string) uint64 {

func getBitArrayFromInt(source uint64, nbOfBits int) []byte {

Check failure on line 107 in token/extended/extended.go

View workflow job for this annotation

GitHub Actions / Lint golangci-lint

func `getBitArrayFromInt` is unused (unused)
bitsArray := make([]byte, (nbOfBits/8)+1)
binary.LittleEndian.PutUint64(bitsArray, source)
binary.BigEndian.PutUint64(bitsArray, source)

return bitsArray
}

func bitArrayToInt(bits []byte) uint64 {
return binary.LittleEndian.Uint64(bits)
return binary.BigEndian.Uint64(bits)
}
242 changes: 242 additions & 0 deletions token/sample_tokens.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
[
{
"serial_number": "TEST220000001",
"starting_code": 516959010,
"key": "bc41ec9530f6dac86b1a29ab82edc5fb",
"token_count": 3,
"restricted_digit_set": false,
"token_type": 1,
"value_raw": 1,
"token": "380589011"
},
{
"serial_number": "TEST220000001",
"starting_code": 516959010,
"key": "bc41ec9530f6dac86b1a29ab82edc5fb",
"token_count": 5,
"restricted_digit_set": false,
"token_type": 1,
"value_raw": 2,
"token": "283675012"
},
{
"serial_number": "TEST220000001",
"starting_code": 516959010,
"key": "bc41ec9530f6dac86b1a29ab82edc5fb",
"token_count": 7,
"restricted_digit_set": false,
"token_type": 1,
"value_raw": 5,
"token": "034254015"
},
{
"serial_number": "TEST220000001",
"starting_code": 516959010,
"key": "bc41ec9530f6dac86b1a29ab82edc5fb",
"token_count": 9,
"restricted_digit_set": false,
"token_type": 1,
"value_raw": 995,
"token": "409152005"
},
{
"serial_number": "TEST220000001",
"starting_code": 516959010,
"key": "bc41ec9530f6dac86b1a29ab82edc5fb",
"token_count": 11,
"restricted_digit_set": false,
"token_type": 1,
"value_raw": 998,
"token": "071763008"
},
{
"serial_number": "TEST220000001",
"starting_code": 516959010,
"key": "bc41ec9530f6dac86b1a29ab82edc5fb",
"token_count": 13,
"restricted_digit_set": false,
"token_type": 1,
"value_raw": 999,
"token": "814704009"
},
{
"serial_number": "TEST220000001",
"starting_code": 516959010,
"key": "bc41ec9530f6dac86b1a29ab82edc5fb",
"token_count": 14,
"restricted_digit_set": false,
"token_type": 2,
"value_raw": 1,
"token": "141465011"
},
{
"serial_number": "TEST220000001",
"starting_code": 516959010,
"key": "bc41ec9530f6dac86b1a29ab82edc5fb",
"token_count": 16,
"restricted_digit_set": false,
"token_type": 2,
"value_raw": 2,
"token": "448320012"
},
{
"serial_number": "TEST220000001",
"starting_code": 516959010,
"key": "bc41ec9530f6dac86b1a29ab82edc5fb",
"token_count": 18,
"restricted_digit_set": false,
"token_type": 2,
"value_raw": 5,
"token": "730651015"
},
{
"serial_number": "TEST220000001",
"starting_code": 516959010,
"key": "bc41ec9530f6dac86b1a29ab82edc5fb",
"token_count": 20,
"restricted_digit_set": false,
"token_type": 2,
"value_raw": 995,
"token": "132820005"
},
{
"serial_number": "TEST220000001",
"starting_code": 516959010,
"key": "bc41ec9530f6dac86b1a29ab82edc5fb",
"token_count": 22,
"restricted_digit_set": false,
"token_type": 2,
"value_raw": 998,
"token": "146345008"
},
{
"serial_number": "TEST220000001",
"starting_code": 516959010,
"key": "bc41ec9530f6dac86b1a29ab82edc5fb",
"token_count": 24,
"restricted_digit_set": false,
"token_type": 2,
"value_raw": 999,
"token": "386863009"
},
{
"serial_number": "TEST240000002",
"starting_code": 432435255,
"key": "dac86b1a29ab82edc5fbbc41ec9530f6",
"token_count": 3,
"restricted_digit_set": true,
"token_type": 1,
"value_raw": 1,
"token": "413441444234331"
},
{
"serial_number": "TEST240000002",
"starting_code": 432435255,
"key": "dac86b1a29ab82edc5fbbc41ec9530f6",
"token_count": 5,
"restricted_digit_set": true,
"token_type": 1,
"value_raw": 2,
"token": "431131331113332"
},
{
"serial_number": "TEST240000002",
"starting_code": 432435255,
"key": "dac86b1a29ab82edc5fbbc41ec9530f6",
"token_count": 7,
"restricted_digit_set": true,
"token_type": 1,
"value_raw": 5,
"token": "423424444232241"
},
{
"serial_number": "TEST240000002",
"starting_code": 432435255,
"key": "dac86b1a29ab82edc5fbbc41ec9530f6",
"token_count": 9,
"restricted_digit_set": true,
"token_type": 1,
"value_raw": 995,
"token": "422313413112333"
},
{
"serial_number": "TEST240000002",
"starting_code": 432435255,
"key": "dac86b1a29ab82edc5fbbc41ec9530f6",
"token_count": 11,
"restricted_digit_set": true,
"token_type": 1,
"value_raw": 998,
"token": "231434142221342"
},
{
"serial_number": "TEST240000002",
"starting_code": 432435255,
"key": "dac86b1a29ab82edc5fbbc41ec9530f6",
"token_count": 13,
"restricted_digit_set": true,
"token_type": 1,
"value_raw": 999,
"token": "242313431134143"
},
{
"serial_number": "TEST240000002",
"starting_code": 432435255,
"key": "dac86b1a29ab82edc5fbbc41ec9530f6",
"token_count": 14,
"restricted_digit_set": true,
"token_type": 2,
"value_raw": 1,
"token": "113434333414311"
},
{
"serial_number": "TEST240000002",
"starting_code": 432435255,
"key": "dac86b1a29ab82edc5fbbc41ec9530f6",
"token_count": 16,
"restricted_digit_set": true,
"token_type": 2,
"value_raw": 2,
"token": "414212121322332"
},
{
"serial_number": "TEST240000002",
"starting_code": 432435255,
"key": "dac86b1a29ab82edc5fbbc41ec9530f6",
"token_count": 18,
"restricted_digit_set": true,
"token_type": 2,
"value_raw": 5,
"token": "413424224321241"
},
{
"serial_number": "TEST240000002",
"starting_code": 432435255,
"key": "dac86b1a29ab82edc5fbbc41ec9530f6",
"token_count": 20,
"restricted_digit_set": true,
"token_type": 2,
"value_raw": 995,
"token": "342124322343233"
},
{
"serial_number": "TEST240000002",
"starting_code": 432435255,
"key": "dac86b1a29ab82edc5fbbc41ec9530f6",
"token_count": 22,
"restricted_digit_set": true,
"token_type": 2,
"value_raw": 998,
"token": "211422314241142"
},
{
"serial_number": "TEST240000002",
"starting_code": 432435255,
"key": "dac86b1a29ab82edc5fbbc41ec9530f6",
"token_count": 24,
"restricted_digit_set": true,
"token_type": 2,
"value_raw": 999,
"token": "331233113332423"
}
]
Loading

0 comments on commit f7e54dd

Please sign in to comment.