-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutil.go
53 lines (44 loc) · 1.23 KB
/
util.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package golpe
import (
"encoding/base64"
"fmt"
"log"
"github.com/jm33-m0/arc"
)
// Base64Encode encodes a byte slice to a base64 URL-encoded string
func Base64Encode(data []byte) string {
return base64.URLEncoding.EncodeToString(data)
}
// Base64Decode decodes a base64 URL-encoded string to a byte slice
func Base64Decode(text string) []byte {
dec, err := base64.URLEncoding.DecodeString(text)
if err != nil {
log.Printf("Base64Decode: %v", err)
return nil
}
return dec
}
// Bin2String compresses a binary file and encodes it with base64
func Bin2String(data []byte) (res string) {
compressedBin, err := arc.Compress(data, arc.CompressionMap["bz2"])
if err != nil {
log.Printf("Bin2String: %v", err)
return
}
// Encode the compressed data to base64
res = Base64Encode(compressedBin)
if res == "" {
log.Println("Bin2String failed, empty string generated")
}
return
}
// ExtractFileFromString base64 decodes and decompresses using BZ2
func ExtractFileFromString(data string) ([]byte, error) {
// Decode base64
decoded := Base64Decode(data)
if len(decoded) == 0 {
return nil, fmt.Errorf("ExtractFileFromString: Failed to decode")
}
// Get the decompressed data
return arc.Decompress(decoded, arc.CompressionMap["bz2"])
}