
Security News
Open Source Maintainers Demand Ability to Block Copilot-Generated Issues and PRs
Open source maintainers are urging GitHub to let them block Copilot from submitting AI-generated issues and pull requests to their repositories.
github.com/goloop/key
Package key allows to find the sequence that will be formed from the permutation of some characters defined in the alphabet by the specified iteration number.
The library can be used to create unique string identifiers based on a numeric identifier, or to create short URLs in redirect systems, or to mask certain indexes in data reports, etc.
Use the arbitrary alphabet and size of key can be created a sequence of unique combinations, where each new combination has its unique numeric index (from 0 to N - where the N is maximum number of possible combinations).
If specify the iteration index (for example, it can be ID field from the some table of database) - will be returned the combination (key) for this index. And if the key is specified - decoding allows to determine the iteration index.
For example, the length of the key is 3 characters and there are several elements to iterate (alphabet): a
, b
and c
. The package allows to answer the following questions:
For "abc" alphabet and 3 key size can be created the next iterations:
0. aaa 1. aab 2. aac 3. aba 4. abb 5. abc
6. aca 7. acb 8. acc 9. baa 10. bab 11. bac
12. bba 13. bbb 14. bbc 15. bca 16. bcb 17. bcc
18. caa 19. cab 20. cac 21. cba 22. cbb 23. cbc
24. cca 25. ccb 26. ccc
So, the maximum number of iterations is 27. For the 10 iteration will correspond to the "bab" sequence and for example the "aba" combination is the 3 iteration.
Install key:
$ go get github.com/goloop/key
Examples:
package main
import (
"fmt"
"github.com/goloop/key"
)
func main() {
ls, _ := key.New("abcde", 3)
v, _ := ls.Marshal(122) // eec
i, _ := ls.Unmarshal("eec") // 122
fmt.Println(v, i)
// Output: eec 122
}
If you specify the key size as 0 - the key length will be from one character to unknown length (depends on the size of the alphabet).
Example:
ls, _ = key.New("abc") // size not specified
ls.Marshal(1) // "b", <nil>
ls.Marshal(10) // "bab", <nil>
ls.Marshal(100) // "bacab", <nil>
ls.Marshal(1000) // "bbabaab", <nil>
ls.Marshal(10000) // "bbbcabbab", <nil>
ls.Marshal(100000) // "bcaacabbcab", <nil>
ls.Marshal(1000000) // "bcbccbacacaab", <nil>
ls.Marshal(10000000) // "caacbbaabbacbab", <nil>
ls.Unmarshal("b") // 1, <nil>
ls.Unmarshal("bab") // 10, <nil>
ls.Unmarshal("bacab") // 100, <nil>
ls.Unmarshal("bbabaab") // 1000, <nil>
ls.Unmarshal("bbbcabbab") // 10000, <nil>
ls.Unmarshal("bcaacabbcab") // 100000, <nil>
ls.Unmarshal("bcbccbacacaab") // 1000000, <nil>
ls.Unmarshal("caacbbaabbacbab") // 10000000, <nil>
New(alphabet string, size ...uint) (*Locksmith, error)
New returns a pointer to a Locksmith object as the first value and an error if something went wrong, or nil as the second value.
As the first argument, the function takes the size of the key. If the size of the key is set to zero, the key size will be dynamic, i.e. the minimum key size will be one character, and the maximum will depend on the length of the alphabet and the possible maximum iteration index.
The second value is the sequence elements for permutation (alphabet). The alphabet mustn't contain duplicate chars or be empty.
The Locksmith struct represents a key generation object. It provides methods for working with keys, including generating keys from IDs and retrieving IDs from keys.
Alphabet() string
The Alphabet
method returns the current alphabet value used by the Locksmith object. The alphabet is a string of unique characters from which the keys will be generated.
Size() uint64
The Size method returns the size of the key set for the Locksmith object. If the size is set to zero, the key size will be dynamic, meaning it can vary depending on the ID.
Total() uint64
The Total method returns the highest possible iteration number for the Locksmith object. It represents the total number of possible keys that can be generated.
Marshal(id uint64) (string, error)
The Marshal method converts an ID into a key. It takes an ID as input and generates a corresponding key based on the Locksmith's alphabet and size.
The id is the ID to be converted into a key.
The method returns a string representing the generated key and an error if something went wrong. If the function is successful, the error will be nil.
Unmarshal(key string) (uint64, error)
The Unmarshal method decodes a key and returns its corresponding ID. It converts a key back into its ID. The key should be a string composed of characters from the Locksmith's alphabet.
The key is the key to be decoded into an ID.
The method returns an integer representing the decoded ID and an error if something went wrong. If the function is successful, the error will be nil.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Open source maintainers are urging GitHub to let them block Copilot from submitting AI-generated issues and pull requests to their repositories.
Research
Security News
Malicious Koishi plugin silently exfiltrates messages with hex strings to a hardcoded QQ account, exposing secrets in chatbots across platforms.
Research
Security News
Malicious PyPI checkers validate stolen emails against TikTok and Instagram APIs, enabling targeted account attacks and dark web credential sales.