gsdDpapiTransform function

Uint8List gsdDpapiTransform(
  1. Uint8List input, {
  2. required bool encrypt,
})

CryptProtectData (encrypt) bzw. CryptUnprotectData im Benutzerkontext. Wirft WindowsException, wenn Windows ablehnt.

Implementation

Uint8List gsdDpapiTransform(Uint8List input, {required bool encrypt}) {
  return using((arena) {
    final data = arena<Uint8>(input.isEmpty ? 1 : input.length);
    data.asTypedList(input.length).setAll(0, input);

    final inBlob = arena<CRYPT_INTEGER_BLOB>();
    inBlob.ref.cbData = input.length;
    inBlob.ref.pbData = data;
    final outBlob = arena<CRYPT_INTEGER_BLOB>();

    final Win32Result(value: ok, error: error) = encrypt
        ? CryptProtectData(inBlob, null, null, null, _uiForbidden, outBlob)
        : CryptUnprotectData(inBlob, null, null, null, _uiForbidden, outBlob);
    if (!ok) {
      throw WindowsException(
        error.toHRESULT(),
        message: encrypt ? 'CryptProtectData' : 'CryptUnprotectData',
      );
    }

    final out = outBlob.ref.pbData;
    try {
      // Kopieren: der Puffer gehört Windows und wird gleich freigegeben.
      return Uint8List.fromList(out.asTypedList(outBlob.ref.cbData));
    } finally {
      LocalFree(HLOCAL(out.cast()));
    }
  });
}