Skip to content

Commit b5cccd9

Browse files
committed
unsafe.Slice does the same thing and is less ugly
1 parent adcf691 commit b5cccd9

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

extensions/accessor/windows.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package accessor
99
import (
1010
"context"
1111
"errors"
12-
"math"
1312
"os"
1413
"path/filepath"
1514
"sync"
@@ -88,21 +87,16 @@ func dpapi(op operation, data []byte) (result []byte, err error) {
8887
switch op {
8988
case decrypt:
9089
// https://learn.microsoft.com/windows/win32/api/dpapi/nf-dpapi-cryptunprotectdata
91-
err = windows.CryptUnprotectData(&in, nil, nil, 0, nil, 1, &out)
90+
err = windows.CryptUnprotectData(&in, nil, nil, 0, nil, windows.CRYPTPROTECT_UI_FORBIDDEN, &out)
9291
case encrypt:
9392
// https://learn.microsoft.com/windows/win32/api/dpapi/nf-dpapi-cryptprotectdata
94-
err = windows.CryptProtectData(&in, nil, nil, 0, nil, 1, &out)
93+
err = windows.CryptProtectData(&in, nil, nil, 0, nil, windows.CRYPTPROTECT_UI_FORBIDDEN, &out)
9594
default:
9695
err = errors.New("invalid operation")
9796
}
9897
if err == nil {
99-
// cast out.Data to a pointer to an arbitrarily long array, then slice the array and copy out.Size bytes from the
100-
// slice to result. This avoids allocating memory for a throwaway buffer but imposes a max size on the data because
101-
// the fictive array backing the slice can't be larger than the address space or the maximum value of an int. Those
102-
// values vary by platform, so the array size here is a compromise for 32-bit systems and allows ~2 GB of data.
10398
result = make([]byte, out.Size)
104-
source := (*[math.MaxInt32 - 1]byte)(unsafe.Pointer(out.Data))[:]
105-
copy(result, source)
99+
copy(result, unsafe.Slice(out.Data, out.Size))
106100
}
107101
return result, err
108102
}

0 commit comments

Comments
 (0)