diff options
author | Runxi Yu <me@runxiyu.org> | 2025-03-23 18:24:46 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-03-23 18:24:46 +0800 |
commit | f9cbee2dcca4f0ebca01d8f37fdddddafbf93d38 (patch) | |
tree | f27bf62f3162b40924881dac071bc9ab230f80ed /identifier.go | |
parent | Reformat (diff) | |
download | powxy-f9cbee2dcca4f0ebca01d8f37fdddddafbf93d38.tar.gz powxy-f9cbee2dcca4f0ebca01d8f37fdddddafbf93d38.tar.zst powxy-f9cbee2dcca4f0ebca01d8f37fdddddafbf93d38.zip |
Rename token.go to identifier.gov0.1.0
Diffstat (limited to 'identifier.go')
-rw-r--r-- | identifier.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/identifier.go b/identifier.go new file mode 100644 index 0000000..6da0e7c --- /dev/null +++ b/identifier.go @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: BSD-2-Clause +// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> + +package main + +import ( + "crypto/hmac" + "crypto/sha256" + "encoding/binary" + "net/http" + "time" +) + +func makeIdentifierMAC(request *http.Request) (identifier []byte, mac []byte) { + identifier = make([]byte, 0, sha256.Size) + mac = make([]byte, 0, sha256.Size) + + timeBuf := make([]byte, binary.MaxVarintLen64) + binary.PutVarint(timeBuf, time.Now().Unix()/604800) + + remoteIP := getRemoteIP(request) + + h := sha256.New() + h.Write(timeBuf) + h.Write(stringToBytes(remoteIP)) + h.Write(stringToBytes(request.Header.Get("User-Agent"))) + h.Write(stringToBytes(request.Header.Get("Accept-Encoding"))) + h.Write(stringToBytes(request.Header.Get("Accept-Language"))) + h.Write(privkeyHash) + identifier = h.Sum(identifier) + if len(identifier) != sha256.Size { + panic("unexpected buffer length after hashing contents") + } + + m := hmac.New(sha256.New, privkey) + m.Write(identifier) + mac = m.Sum(mac) + if len(mac) != sha256.Size { + panic("unexpected buffer length after hmac") + } + + return +} |