Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pinax-network/signedkeys
Go library to generate, sign, verify and encode keys (such as api keys).
https://github.com/pinax-network/signedkeys
Last synced: about 2 months ago
JSON representation
Go library to generate, sign, verify and encode keys (such as api keys).
- Host: GitHub
- URL: https://github.com/pinax-network/signedkeys
- Owner: pinax-network
- License: mit
- Created: 2024-04-03T20:36:29.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-04-03T21:09:17.000Z (9 months ago)
- Last Synced: 2024-06-19T18:38:25.239Z (7 months ago)
- Language: Go
- Size: 8.79 KB
- Stars: 0
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Signed Keys
This Go library generates, signs, validates and encodes keys, such as api keys for example. This allows such keys to be
pre-validated in a fast and cheap way. An exemplary use case could be high throughput API endpoints that might not
be easily rate-limited, but you still want to prevent bad actors from overloading your database by spamming requests
with random api keys.```
go get github.com/pinax-network/signedkeys
```## Example usage
```golang
package mainimport (
"crypto/sha1"
"fmt"
"github.com/pinax-network/signedkeys"
)var keyGen *signedkeys.Generator
func init() {
// initializes a new key generator using
keyGen = signedkeys.NewGenerator(
signedkeys.WithRand(signedkeys.Secure()), // a secure random generator for the key generation from crypto/rand (this is the default)
signedkeys.WithKeyLength(12), // a key length of 12 bytes
signedkeys.WithSigner(signedkeys.HmacSigner(sha1.New, []byte("my_secret"), 8)), // a signer that adds an 8-byte hmac signature using sha1 hashes and "my_secret" as secret
signedkeys.WithVerifier(signedkeys.HmacVerifier(sha1.New, []byte("my_secret"), 8)), // the appropriate hmac validator with the same settings as the signer
signedkeys.WithEncoding(signedkeys.HexEncoding()), // hexadecimal encoding for the resulting key + signature
)
}func main() {
// generates a new key
key, err := keyGen.GenerateKey()
if err != nil {
panic(err)
}
fmt.Printf("generated key: %s\n", key)// use the key generator to validate the keys signature
valid := keyGen.VerifySignature(key)
fmt.Printf("is valid: %t\n", valid)
}
```