Browse Source

formatted files, fixed linter warnings.

remove-cryptography-dependency
Herbert Poul 5 years ago
parent
commit
8f7fc702eb
  1. 3
      bin/kdbx.dart
  2. 4
      example/pubspec.lock
  3. 3
      lib/kdbx.dart
  4. 6
      lib/src/internal/async_utils.dart
  5. 27
      lib/src/internal/crypto_utils.dart
  6. 3
      lib/src/kdbx_format.dart
  7. 8
      lib/src/kdbx_times.dart

3
bin/kdbx.dart

@ -1,6 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'dart:typed_data';
import 'package:args/args.dart'; import 'package:args/args.dart';
import 'package:args/command_runner.dart'; import 'package:args/command_runner.dart';
@ -72,7 +71,7 @@ abstract class KdbxFileCommand extends Command<void> {
if (inputFile == null) { if (inputFile == null) {
usageException('Required argument: --input'); usageException('Required argument: --input');
} }
final bytes = await File(inputFile).readAsBytes() as Uint8List; final bytes = await File(inputFile).readAsBytes();
final password = prompts.get('Password for $inputFile', final password = prompts.get('Password for $inputFile',
conceal: true, validate: (str) => str.isNotEmpty); conceal: true, validate: (str) => str.isNotEmpty);
final file = KdbxFormat.read( final file = KdbxFormat.read(

4
example/pubspec.lock

@ -82,7 +82,7 @@ packages:
path: ".." path: ".."
relative: true relative: true
source: path source: path
version: "0.1.0" version: "0.2.1"
logging: logging:
dependency: transitive dependency: transitive
description: description:
@ -103,7 +103,7 @@ packages:
name: meta name: meta
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.7" version: "1.1.8"
path: path:
dependency: transitive dependency: transitive
description: description:

3
lib/kdbx.dart

@ -1,7 +1,8 @@
/// dart library for reading keepass file format (kdbx). /// dart library for reading keepass file format (kdbx).
library kdbx; library kdbx;
export 'src/crypto/protected_value.dart' show ProtectedValue, StringValue, PlainValue; export 'src/crypto/protected_value.dart'
show ProtectedValue, StringValue, PlainValue;
export 'src/kdbx_consts.dart'; export 'src/kdbx_consts.dart';
export 'src/kdbx_entry.dart'; export 'src/kdbx_entry.dart';
export 'src/kdbx_format.dart'; export 'src/kdbx_format.dart';

6
lib/src/internal/async_utils.dart

@ -3,10 +3,12 @@ import 'dart:async';
/// Base class which can be used as a mixin directly, but you have to call `cancelSubscriptions`. /// Base class which can be used as a mixin directly, but you have to call `cancelSubscriptions`.
/// If used inside a [State], use [StreamSubscriberMixin]. /// If used inside a [State], use [StreamSubscriberMixin].
mixin StreamSubscriberBase { mixin StreamSubscriberBase {
final List<StreamSubscription<dynamic>> _subscriptions = <StreamSubscription<dynamic>>[]; final List<StreamSubscription<dynamic>> _subscriptions =
<StreamSubscription<dynamic>>[];
/// Listens to a stream and saves it to the list of subscriptions. /// Listens to a stream and saves it to the list of subscriptions.
void listen(Stream<dynamic> stream, void onData(dynamic data), {Function onError}) { void listen(Stream<dynamic> stream, void onData(dynamic data),
{Function onError}) {
if (stream != null) { if (stream != null) {
_subscriptions.add(stream.listen(onData, onError: onError)); _subscriptions.add(stream.listen(onData, onError: onError));
} }

27
lib/src/internal/crypto_utils.dart

@ -1,5 +1,3 @@
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:pointycastle/export.dart'; import 'package:pointycastle/export.dart';
@ -14,25 +12,29 @@ class AesHelper {
static const ITERATION_COUNT = 1000; static const ITERATION_COUNT = 1000;
static Uint8List deriveKey( static Uint8List deriveKey(
Uint8List password, { Uint8List password, {
Uint8List salt, Uint8List salt,
int iterationCount = ITERATION_COUNT, int iterationCount = ITERATION_COUNT,
int derivedKeyLength = KEY_SIZE, int derivedKeyLength = KEY_SIZE,
}) { }) {
final Pbkdf2Parameters params = Pbkdf2Parameters(salt, iterationCount, derivedKeyLength); final Pbkdf2Parameters params =
final KeyDerivator keyDerivator = PBKDF2KeyDerivator(HMac(SHA256Digest(), 16)); Pbkdf2Parameters(salt, iterationCount, derivedKeyLength);
final KeyDerivator keyDerivator =
PBKDF2KeyDerivator(HMac(SHA256Digest(), 16));
keyDerivator.init(params); keyDerivator.init(params);
return keyDerivator.process(password); return keyDerivator.process(password);
} }
static String decrypt(Uint8List derivedKey, Uint8List cipherIvBytes, {String mode = CBC_MODE}) { static String decrypt(Uint8List derivedKey, Uint8List cipherIvBytes,
{String mode = CBC_MODE}) {
// Uint8List derivedKey = deriveKey(password); // Uint8List derivedKey = deriveKey(password);
final KeyParameter keyParam = KeyParameter(derivedKey); final KeyParameter keyParam = KeyParameter(derivedKey);
final BlockCipher aes = AESFastEngine(); final BlockCipher aes = AESFastEngine();
// Uint8List cipherIvBytes = base64.decode(ciphertext); // Uint8List cipherIvBytes = base64.decode(ciphertext);
final Uint8List iv = Uint8List(aes.blockSize)..setRange(0, aes.blockSize, cipherIvBytes); final Uint8List iv = Uint8List(aes.blockSize)
..setRange(0, aes.blockSize, cipherIvBytes);
BlockCipher cipher; BlockCipher cipher;
final ParametersWithIV params = ParametersWithIV(keyParam, iv); final ParametersWithIV params = ParametersWithIV(keyParam, iv);
@ -50,7 +52,8 @@ class AesHelper {
cipher.init(false, params); cipher.init(false, params);
final int cipherLen = cipherIvBytes.length - aes.blockSize; final int cipherLen = cipherIvBytes.length - aes.blockSize;
final Uint8List cipherBytes = Uint8List(cipherLen)..setRange(0, cipherLen, cipherIvBytes, aes.blockSize); final Uint8List cipherBytes = Uint8List(cipherLen)
..setRange(0, cipherLen, cipherIvBytes, aes.blockSize);
final Uint8List paddedText = processBlocks(cipher, cipherBytes); final Uint8List paddedText = processBlocks(cipher, cipherBytes);
final Uint8List textBytes = unpad(paddedText); final Uint8List textBytes = unpad(paddedText);

3
lib/src/kdbx_format.dart

@ -30,8 +30,6 @@ abstract class Credentials {
keyFile: keyFile == null ? null : KeyFileCredentials(keyFile), keyFile: keyFile == null ? null : KeyFileCredentials(keyFile),
); );
Credentials._();
factory Credentials.fromHash(Uint8List hash) => HashCredentials(hash); factory Credentials.fromHash(Uint8List hash) => HashCredentials(hash);
Uint8List getHash(); Uint8List getHash();
@ -42,6 +40,7 @@ class KeyFileComposite implements Credentials {
PasswordCredentials password; PasswordCredentials password;
KeyFileCredentials keyFile; KeyFileCredentials keyFile;
@override
Uint8List getHash() { Uint8List getHash() {
final buffer = [...?password?.getBinary(), ...?keyFile?.getBinary()]; final buffer = [...?password?.getBinary(), ...?keyFile?.getBinary()];
return crypto.sha256.convert(buffer).bytes as Uint8List; return crypto.sha256.convert(buffer).bytes as Uint8List;

8
lib/src/kdbx_times.dart

@ -1,5 +1,3 @@
import 'package:clock/clock.dart'; import 'package:clock/clock.dart';
import 'package:kdbx/src/kdbx_object.dart'; import 'package:kdbx/src/kdbx_object.dart';
import 'package:kdbx/src/kdbx_xml.dart'; import 'package:kdbx/src/kdbx_xml.dart';
@ -19,12 +17,14 @@ class KdbxTimes extends KdbxNode {
KdbxTimes.read(XmlElement node) : super.read(node); KdbxTimes.read(XmlElement node) : super.read(node);
DateTimeUtcNode get creationTime => DateTimeUtcNode(this, 'CreationTime'); DateTimeUtcNode get creationTime => DateTimeUtcNode(this, 'CreationTime');
DateTimeUtcNode get lastModificationTime => DateTimeUtcNode(this, 'CreationTime'); DateTimeUtcNode get lastModificationTime =>
DateTimeUtcNode(this, 'CreationTime');
DateTimeUtcNode get lastAccessTime => DateTimeUtcNode(this, 'CreationTime'); DateTimeUtcNode get lastAccessTime => DateTimeUtcNode(this, 'CreationTime');
DateTimeUtcNode get expiryTime => DateTimeUtcNode(this, 'CreationTime'); DateTimeUtcNode get expiryTime => DateTimeUtcNode(this, 'CreationTime');
BooleanNode get expires => BooleanNode(this, 'Expires'); BooleanNode get expires => BooleanNode(this, 'Expires');
IntNode get usageCount => IntNode(this, 'Usagecount'); IntNode get usageCount => IntNode(this, 'Usagecount');
DateTimeUtcNode get locationChanged => DateTimeUtcNode(this, 'LocationChanged'); DateTimeUtcNode get locationChanged =>
DateTimeUtcNode(this, 'LocationChanged');
void accessedNow() { void accessedNow() {
lastAccessTime.set(clock.now().toUtc()); lastAccessTime.set(clock.now().toUtc());

Loading…
Cancel
Save