-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytes.go
112 lines (90 loc) · 2.66 KB
/
bytes.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2022 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package cryptox
import (
"crypto/rand"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"os"
)
var (
// FileFlag is the flag of file.
FileFlag = os.O_CREATE | os.O_APPEND | os.O_WRONLY
// FileMode is the mode of file.
FileMode os.FileMode = 0644
)
var (
// Base64 is the encoding of base64.
Base64 = base64.StdEncoding
)
// Bytes is an alias of []byte.
type Bytes []byte
// Bytes returns Bytes in []byte.
func (bs Bytes) Bytes() []byte {
return bs
}
// String returns Bytes in string.
func (bs Bytes) String() string {
return string(bs)
}
// Hex returns Bytes in hex.
func (bs Bytes) Hex() string {
return hex.EncodeToString(bs)
}
// Base64 returns Bytes in base64.
func (bs Bytes) Base64() string {
return Base64.EncodeToString(bs)
}
// Clone clones bs to new slice.
func (bs Bytes) Clone() Bytes {
newSlice := make([]byte, len(bs))
copy(newSlice, bs)
return newSlice
}
// newFile creates a new file of path.
func (bs Bytes) newFile(path string) (*os.File, error) {
return os.OpenFile(path, FileFlag, FileMode)
}
// WriteTo writes bytes to writer.
func (bs Bytes) WriteTo(writer io.Writer) (n int64, err error) {
nn, err := writer.Write(bs)
return int64(nn), err
}
// WriteToFile writes bytes to file.
func (bs Bytes) WriteToFile(path string) (n int64, err error) {
file, err := bs.newFile(path)
if err != nil {
return 0, err
}
defer file.Close()
return bs.WriteTo(file)
}
// ParseHex uses hex to parse string to Bytes.
func ParseHex(str string) (Bytes, error) {
return hex.DecodeString(str)
}
// ParseBase64 uses base64 to parse string to Bytes.
func ParseBase64(str string) (Bytes, error) {
return Base64.DecodeString(str)
}
// RandomBytes returns a byte slice filled with random byte.
// It usually used to generate an iv and install iv to crypted data.
// For example, you use this method to generate a byte slice and pass it to encrypter as iv.
// After encrypting, you append this iv slice encoded to hex or base64 to crypted slice as they are one part.
// When you need to decrypt data, parse iv from the "crypted" slice including raw-crypted slice and iv slice first.
// Then you can pass this iv to decrypter and decrypt data as usual.
// However, you should know that the crypted data of the same plain data will be different every time because of different ivs.
func RandomBytes(n int) (Bytes, error) {
bs := make([]byte, n)
read, err := rand.Read(bs)
if err != nil {
return nil, err
}
if read != n {
return nil, fmt.Errorf("cryptox.RandomBytes: read %d != n %d", read, n)
}
return bs, nil
}