dvPrivacyKeyFrom function

List<int> dvPrivacyKeyFrom(
  1. String raw
)

The bytes of a privacy signing key written as hex or base64.

Throws StateError for anything shorter than 32 bytes, naming DVPrivacyRuntime.keyVariable: a short key would still sign and still derive pseudonyms, just ones somebody could reverse.

Implementation

List<int> dvPrivacyKeyFrom(String raw) {
  final String text = raw.trim();
  List<int>? bytes;
  if (RegExp(r'^(?:[0-9a-fA-F]{2})+$').hasMatch(text)) {
    bytes = <int>[
      for (int i = 0; i < text.length; i += 2)
        int.parse(text.substring(i, i + 2), radix: 16),
    ];
  } else {
    try {
      bytes = base64.decode(base64.normalize(text.replaceAll('-', '+')
          .replaceAll('_', '/')));
    } on FormatException {
      bytes = null;
    }
  }
  if (bytes == null || bytes.length < 32) {
    throw StateError('${DVPrivacyRuntime.keyVariable} must decode, as hex or '
        'base64, to at least 32 bytes'
        '${bytes == null ? '' : '; it decodes to ${bytes.length}'}.');
  }
  return bytes;
}