Debian Codegraph @

ntlm/helpers.go

raw symbols SCIP
1 //Copyright 2013 Thomson Reuters Global Resources. BSD License please see License file for more information
2
3 package ntlm
4
5 import (
6 "bytes"
7 "crypto/rand"
8 "encoding/binary"
9 "unicode/utf16"
10 )
11
12 // Concatenate two byte slices into a new slice
13 func concat(ar ...[]byte) []byte {
14 return bytes.Join(ar, nil)
15 }
16
17 // Create a 0 initialized slice of bytes
18 func zeroBytes(length int) []byte {
19 return make([]byte, length, length)
20 }
21
22 func randomBytes(length int) []byte {
23 randombytes := make([]byte, length)
24 _, err := rand.Read(randombytes)
25 if err != nil {
26 } // TODO: What to do with err here
27 return randombytes
28 }
29
30 // Zero pad the input byte slice to the given size
31 // bytes - input byte slice
32 // offset - where to start taking the bytes from the input slice
33 // size - size of the output byte slize
34 func zeroPaddedBytes(bytes []byte, offset int, size int) []byte {
35 newSlice := zeroBytes(size)
36 for i := 0; i < size && i+offset < len(bytes); i++ {
37 newSlice[i] = bytes[i+offset]
38 }
39 return newSlice
40 }
41
42 func MacsEqual(slice1, slice2 []byte) bool {
43 if len(slice1) != len(slice2) {
44 return false
45 }
46 for i := 0; i < len(slice1); i++ {
47 // bytes between 4 and 7 (inclusive) contains random
48 // data that should be ignored while comparing the
49 // macs
50 if (i < 4 || i > 7) && slice1[i] != slice2[i] {
51 return false
52 }
53 }
54 return true
55 }
56
57 func utf16FromString(s string) []byte {
58 encoded := utf16.Encode([]rune(s))
59 // TODO: I'm sure there is an easier way to do the conversion from utf16 to bytes
60 result := zeroBytes(len(encoded) * 2)
61 for i := 0; i < len(encoded); i++ {
62 result[i*2] = byte(encoded[i])
63 result[i*2+1] = byte(encoded[i] << 8)
64 }
65 return result
66 }
67
68 // Convert a UTF16 string to UTF8 string for Go usage
69 func utf16ToString(bytes []byte) string {
70 var data []uint16
71
72 // NOTE: This is definitely not the best way to do this, but when I tried using a buffer.Read I could not get it to work
73 for offset := 0; offset < len(bytes); offset = offset + 2 {
74 i := binary.LittleEndian.Uint16(bytes[offset : offset+2])
75 data = append(data, i)
76 }
77
78 return string(utf16.Decode(data))
79 }
80
81 func uint32ToBytes(v uint32) []byte {
82 bytes := make([]byte, 4)
83 bytes[0] = byte(v & 0xff)
84 bytes[1] = byte((v >> 8) & 0xff)
85 bytes[2] = byte((v >> 16) & 0xff)
86 bytes[3] = byte((v >> 24) & 0xff)
87 return bytes
88 }