Debian Codegraph @

arrays/arrays.go

raw symbols SCIP
1 // Package arrays provides various byte array utilities
2 package arrays
3
4 import (
5 "bytes"
6 "crypto/rand"
7 "encoding/binary"
8 "fmt"
9 "github.com/dvsekhvalnov/jose2go/base64url"
10 )
11
12 // Xor is doing byte by byte exclusive or of 2 byte arrays
13 func Xor(left, right []byte) []byte {
14 result := make([]byte, len(left))
15
16 for i := 0; i < len(left); i++ {
17 result[i] = left[i] ^ right[i]
18 }
19
20 return result
21 }
22
23 // Slice is splitting input byte array into slice of subarrays. Each of count length.
24 func Slice(arr []byte, count int) ([][]byte, error) {
25
26 if len(arr) == 0 || len(arr) % count != 0 {
27 return nil, fmt.Errorf("arrays.Slice() expects input data padded up to %v bytes, but was given %v bytes", count, len(arr))
28
29 }
30
31 sliceCount := len(arr) / count
32
33 result := make([][]byte, sliceCount)
34
35 for i := 0; i < sliceCount; i++ {
36 start := i * count
37 end := i*count + count
38
39 result[i] = arr[start:end]
40 }
41
42 return result, nil
43 }
44
45 // Random generates byte array with random data of byteCount length
46 func Random(byteCount int) ([]byte, error) {
47 data := make([]byte, byteCount)
48
49 if _, err := rand.Read(data); err != nil {
50 return nil, err
51 }
52
53 return data, nil
54 }
55
56 // Concat combine several arrays into single one, resulting slice = A1 | A2 | A3 | ... | An
57 func Concat(arrays ...[]byte) []byte {
58 var result []byte = arrays[0]
59
60 for _, arr := range arrays[1:] {
61 result = append(result, arr...)
62 }
63
64 return result
65 }
66
67 // Unwrap same thing as Contact, just different interface, combines several array into single one
68 func Unwrap(arrays [][]byte) []byte {
69 var result []byte = arrays[0]
70
71 for _, arr := range arrays[1:] {
72 result = append(result, arr...)
73 }
74
75 return result
76 }
77
78 // UInt64ToBytes unwrap uint64 value to byte array of length 8 using big endian
79 func UInt64ToBytes(value uint64) []byte {
80 result := make([]byte, 8)
81 binary.BigEndian.PutUint64(result, value)
82
83 return result
84 }
85
86 // UInt32ToBytes unwrap uint32 value to byte array of length 4 using big endian
87 func UInt32ToBytes(value uint32) []byte {
88 result := make([]byte, 4)
89 binary.BigEndian.PutUint32(result, value)
90
91 return result
92 }
93
94 // Dump produces printable debug representation of byte array as string
95 func Dump(arr []byte) string {
96 var buf bytes.Buffer
97
98 buf.WriteString("(")
99 buf.WriteString(fmt.Sprintf("%v", len(arr)))
100 buf.WriteString(" bytes)[")
101
102 for idx, b := range arr {
103 buf.WriteString(fmt.Sprintf("%v", b))
104 if idx != len(arr)-1 {
105 buf.WriteString(", ")
106 }
107 }
108
109 buf.WriteString("], Hex: [")
110
111 for idx, b := range arr {
112 buf.WriteString(fmt.Sprintf("%X", b))
113 if idx != len(arr)-1 {
114 buf.WriteString(" ")
115 }
116 }
117
118 buf.WriteString("], Base64Url:")
119 buf.WriteString(base64url.Encode(arr))
120
121 return buf.String()
122 }