forked from cactus/go-camo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
go-base64.go
39 lines (32 loc) · 871 Bytes
/
go-base64.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) 2012-2023 Eli Janssen
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
//go:build ignore
// +build ignore
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"strings"
)
var CAMO_HOST = "https://img.example.com"
func b64encode(data []byte) string {
return strings.TrimRight(base64.URLEncoding.EncodeToString(data), "=")
}
func GenCamoUrl(hmacKey []byte, srcUrl string) string {
if strings.HasPrefix(srcUrl, "https:") {
return srcUrl
}
oBytes := []byte(srcUrl)
mac := hmac.New(sha1.New, hmacKey)
mac.Write(oBytes)
macSum := b64encode(mac.Sum(nil))
encodedUrl := b64encode(oBytes)
encurl := CAMO_HOST + "/" + macSum + "/" + encodedUrl
return encurl
}
func main() {
fmt.Println(GenCamoUrl([]byte("test"), "http://golang.org/doc/gopher/frontpage.png"))
}