main.go raw
1 package main
2
3 import (
4 "compress/bzip2"
5 "compress/gzip"
6 "compress/lzw"
7 "compress/zlib"
8 "container/list"
9 "container/ring"
10 "crypto/aes"
11 "crypto/des"
12 "crypto/hmac"
13 "crypto/md5"
14 "crypto/rand"
15 "crypto/sha1"
16 "crypto/sha256"
17 "crypto/sha512"
18 "hash/crc32"
19 "hash/crc64"
20 "image"
21 "image/color"
22 "image/draw"
23 "image/gif"
24 "math"
25 mrand "math/rand/v2"
26 "slices"
27 "sort"
28 )
29
30 func main() {
31 msg := []byte("moxie moxie moxie moxie moxie moxie")
32
33 // Container/list
34 {
35 l := list.New()
36 l.PushBack(42)
37 l.PushBack(99)
38 l.PushBack(7)
39 print("list: ")
40 for e := l.Front(); e != nil; e = e.Next() {
41 print(e.Value.(int))
42 print(" ")
43 }
44 println()
45 }
46
47 // Container/ring
48 {
49 r := ring.New(3)
50 for i := 0; i < 3; i++ {
51 r.Value = i + 1
52 r = r.Next()
53 }
54 sum := 0
55 r.Do(func(v interface{}) {
56 sum += v.(int)
57 })
58 print("ring sum: ")
59 println(sum)
60 }
61
62 // Math
63 {
64 if math.Sqrt(2.0) > 1.4 && math.Sqrt(2.0) < 1.5 {
65 println("math: ok")
66 }
67 }
68
69 // Math/rand
70 {
71 print("rand: ")
72 println(mrand.IntN(100))
73 }
74
75 // LZW
76 {
77 var buf byteBuffer
78 w := lzw.NewWriter(&buf, lzw.LSB, 8)
79 w.Write(msg)
80 w.Close()
81 r := lzw.NewReader(&byteReader{data: buf.data}, lzw.LSB, 8)
82 var result []byte
83 var tmp [256]byte
84 for {
85 n, err := r.Read(tmp[:])
86 if n > 0 {
87 result = append(result, tmp[:n]...)
88 }
89 if err != nil {
90 break
91 }
92 }
93 r.Close()
94 if bytesEqual(result, msg) {
95 println("lzw: PASS")
96 }
97 }
98
99 // Gzip
100 {
101 var buf byteBuffer
102 w := gzip.NewWriter(&buf)
103 w.Write(msg)
104 w.Close()
105 r, _ := gzip.NewReader(&byteReader{data: buf.data})
106 var result []byte
107 var tmp [256]byte
108 for {
109 n, err := r.Read(tmp[:])
110 if n > 0 {
111 result = append(result, tmp[:n]...)
112 }
113 if err != nil {
114 break
115 }
116 }
117 r.Close()
118 if bytesEqual(result, msg) {
119 println("gzip: PASS")
120 }
121 }
122
123 // Zlib
124 {
125 var buf byteBuffer
126 w := zlib.NewWriter(&buf)
127 w.Write(msg)
128 w.Close()
129 r, _ := zlib.NewReader(&byteReader{data: buf.data})
130 var result []byte
131 var tmp [256]byte
132 for {
133 n, err := r.Read(tmp[:])
134 if n > 0 {
135 result = append(result, tmp[:n]...)
136 }
137 if err != nil {
138 break
139 }
140 }
141 r.Close()
142 if bytesEqual(result, msg) {
143 println("zlib: PASS")
144 }
145 }
146
147 // Crypto
148 {
149 h := sha256.Sum256(msg)
150 print("sha256: ")
151 printHexN(h[:], 8)
152 println()
153 }
154 {
155 h := sha512.Sum512(msg)
156 print("sha512: ")
157 printHexN(h[:], 8)
158 println()
159 }
160 {
161 mac := hmac.New(sha256.New, []byte("secret"))
162 mac.Write(msg)
163 h := mac.Sum(nil)
164 print("hmac: ")
165 printHexN(h, 8)
166 println()
167 }
168 {
169 print("crc32: ")
170 printHex32(crc32.ChecksumIEEE(msg))
171 println()
172 }
173 {
174 tab := crc64.MakeTable(crc64.ECMA)
175 print("crc64: ")
176 printHex64(crc64.Checksum(msg, tab))
177 println()
178 }
179 {
180 var key [16]byte
181 rand.Read(key[:])
182 block, _ := aes.NewCipher(key[:])
183 print("aes: block_size=")
184 println(block.BlockSize())
185 }
186
187 // MD5
188 {
189 h := md5.Sum(msg)
190 print("md5: ")
191 printHexN(h[:], 8)
192 println()
193 }
194
195 // SHA1
196 {
197 h := sha1.Sum(msg)
198 print("sha1: ")
199 printHexN(h[:], 8)
200 println()
201 }
202
203 // Bzip2 (decompress only — bzip2 package is decompression only)
204 {
205 // A minimal bzip2 compressed stream for "hello"
206 // Use pre-computed bzip2 data
207 // bzip2 of "hello"
208 compressed := []byte{
209 0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26,
210 0x53, 0x59, 0x19, 0x31, 0x65, 0x3d, 0x00, 0x00,
211 0x00, 0x81, 0x00, 0x02, 0x44, 0xa0, 0x00, 0x21,
212 0x9a, 0x68, 0x33, 0x4d, 0x07, 0x33, 0x8b, 0xb9,
213 0x22, 0x9c, 0x28, 0x48, 0x0c, 0x98, 0xb2, 0x9e,
214 0x80,
215 }
216 r := bzip2.NewReader(&byteReader{data: compressed})
217 var result []byte
218 var tmp [256]byte
219 for {
220 n, err := r.Read(tmp[:])
221 if n > 0 {
222 result = append(result, tmp[:n]...)
223 }
224 if err != nil {
225 break
226 }
227 }
228 print("bzip2: ")
229 if len(result) > 0 {
230 println("PASS")
231 } else {
232 println("FAIL")
233 }
234 }
235
236 // Sort
237 {
238 data := []int{5, 3, 1, 4, 2}
239 sort.Ints(data)
240 sorted := true
241 for i := 0; i < len(data)-1; i++ {
242 if data[i] > data[i+1] {
243 sorted = false
244 }
245 }
246 print("sort: ")
247 if sorted && data[0] == 1 && data[4] == 5 {
248 println("PASS")
249 } else {
250 println("FAIL")
251 }
252 }
253
254 // DES
255 {
256 var key [8]byte
257 rand.Read(key[:])
258 block, err := des.NewCipher(key[:])
259 print("des: ")
260 if err == nil && block.BlockSize() == 8 {
261 println("block_size=8")
262 } else {
263 println("FAIL")
264 }
265 }
266
267 // Slices
268 {
269 data := []int{9, 1, 5, 3, 7}
270 slices.Sort(data)
271 print("slices: ")
272 if slices.IsSorted(data) && data[0] == 1 && data[4] == 9 {
273 println("PASS")
274 } else {
275 println("FAIL")
276 }
277 }
278
279 // Slice concat (| operator)
280 {
281 a := []int{1, 2, 3}
282 b := []int{4, 5}
283 c := a | b
284 print("concat: ")
285 if len(c) == 5 && c[0] == 1 && c[3] == 4 && c[4] == 5 {
286 // Verify a is unchanged (| must not modify the original).
287 if len(a) == 3 && a[0] == 1 {
288 println("PASS")
289 } else {
290 println("FAIL (src mutated)")
291 }
292 } else {
293 println("FAIL")
294 }
295
296 // String ([]byte) concat with |
297 s := "hello" | " world"
298 print("strcat: ")
299 if len(s) == 11 {
300 println("PASS")
301 } else {
302 println("FAIL")
303 }
304 }
305
306 // Image/draw
307 {
308 dst := image.NewRGBA(image.Rect(0, 0, 4, 4))
309 red := image.NewUniform(color.RGBA{R: 255, A: 255})
310 draw.Draw(dst, dst.Bounds(), red, image.Point{}, draw.Src)
311 r, _, _, _ := dst.At(2, 2).RGBA()
312 print("draw: ")
313 if r>>8 == 255 {
314 println("PASS")
315 } else {
316 println("FAIL")
317 }
318 }
319
320 // Image/gif (encode a minimal animated GIF)
321 {
322 var buf byteBuffer
323 frame1 := image.NewPaletted(image.Rect(0, 0, 2, 2), color.Palette{
324 color.RGBA{R: 255, A: 255},
325 color.RGBA{G: 255, A: 255},
326 })
327 frame1.SetColorIndex(0, 0, 1)
328 g := &gif.GIF{
329 Image: []*image.Paletted{frame1},
330 Delay: []int{10},
331 }
332 err := gif.EncodeAll(&buf, g)
333 print("gif: ")
334 if err == nil && len(buf.data) > 0 {
335 println("PASS")
336 } else {
337 println("FAIL")
338 }
339 }
340 }
341
342 func bytesEqual(a, b []byte) bool {
343 if len(a) != len(b) {
344 return false
345 }
346 for i := range a {
347 if a[i] != b[i] {
348 return false
349 }
350 }
351 return true
352 }
353
354 type byteBuffer struct{ data []byte }
355
356 func (b *byteBuffer) Write(p []byte) (int, error) {
357 b.data = append(b.data, p...)
358 return len(p), nil
359 }
360
361 type byteReader struct {
362 data []byte
363 pos int
364 }
365
366 func (r *byteReader) Read(p []byte) (int, error) {
367 if r.pos >= len(r.data) {
368 return 0, eof{}
369 }
370 n := copy(p, r.data[r.pos:])
371 r.pos += n
372 return n, nil
373 }
374
375 type eof struct{}
376
377 func (eof) Error() string { return "EOF" }
378
379 const hexchars = "0123456789abcdef"
380
381 func printHex(b byte) {
382 print([]byte{hexchars[b>>4]})
383 print([]byte{hexchars[b&0x0f]})
384 }
385
386 func printHexN(data []byte, n int) {
387 for i := 0; i < n && i < len(data); i++ {
388 printHex(data[i])
389 }
390 print("...")
391 }
392
393 func printHex32(v uint32) {
394 for i := 28; i >= 0; i -= 4 {
395 print([]byte{hexchars[byte((v>>uint32(i))&0x0f)]})
396 }
397 }
398
399 func printHex64(v uint64) {
400 for i := 60; i >= 0; i -= 4 {
401 print([]byte{hexchars[byte((v>>uint64(i))&0x0f)]})
402 }
403 }
404