libsignal_protocol_dart 0.5.0-nullsafety.0 copy "libsignal_protocol_dart: ^0.5.0-nullsafety.0" to clipboard
libsignal_protocol_dart: ^0.5.0-nullsafety.0 copied to clipboard

outdated

Signal Protocol libray for Dart native and Flutter, pure Dart implementation of the the Signal Protocol

example/main.dart

import 'dart:convert';
import 'dart:typed_data';

import 'package:libsignal_protocol_dart/libsignal_protocol_dart.dart';

Future<void> main() async {
  await install();
}

Future<void> install() async {
  final identityKeyPair = generateIdentityKeyPair();
  final registrationId = generateRegistrationId(false);

  final preKeys = generatePreKeys(0, 110);

  final signedPreKey = generateSignedPreKey(identityKeyPair, 0);

  final sessionStore = InMemorySessionStore();
  final preKeyStore = InMemoryPreKeyStore();
  final signedPreKeyStore = InMemorySignedPreKeyStore();
  final identityStore =
      InMemoryIdentityKeyStore(identityKeyPair, registrationId);

  for (var p in preKeys) {
    await preKeyStore.storePreKey(p.id, p);
  }
  await signedPreKeyStore.storeSignedPreKey(signedPreKey.id, signedPreKey);

  final bobAddress = SignalProtocolAddress('bob', 1);
  final sessionBuilder = SessionBuilder(
      sessionStore, preKeyStore, signedPreKeyStore, identityStore, bobAddress);

  // Should get remote from the server
  final remoteRegId = generateRegistrationId(false);
  final remoteIdentityKeyPair = generateIdentityKeyPair();
  final remotePreKeys = generatePreKeys(0, 110);
  final remoteSignedPreKey = generateSignedPreKey(remoteIdentityKeyPair, 0);

  final retrievedPreKey = PreKeyBundle(
      remoteRegId,
      1,
      remotePreKeys[0].id,
      remotePreKeys[0].getKeyPair().publicKey,
      remoteSignedPreKey.id,
      remoteSignedPreKey.getKeyPair().publicKey,
      remoteSignedPreKey.signature,
      remoteIdentityKeyPair.getPublicKey());

  await sessionBuilder.processPreKeyBundle(retrievedPreKey);

  final sessionCipher = SessionCipher(
      sessionStore, preKeyStore, signedPreKeyStore, identityStore, bobAddress);
  final ciphertext = await sessionCipher
      .encrypt(Uint8List.fromList(utf8.encode('Hello Mixin🤣')));
  // ignore: avoid_print
  print(ciphertext);
  // ignore: avoid_print
  print(ciphertext.serialize());
  //deliver(ciphertext);

  final signalProtocolStore =
      InMemorySignalProtocolStore(remoteIdentityKeyPair, 1);
  final aliceAddress = SignalProtocolAddress('alice', 1);
  final remoteSessionCipher =
      SessionCipher.fromStore(signalProtocolStore, aliceAddress);

  for (var p in remotePreKeys) {
    await signalProtocolStore.storePreKey(p.id, p);
  }
  await signalProtocolStore.storeSignedPreKey(
      remoteSignedPreKey.id, remoteSignedPreKey);

  if (ciphertext.getType() == CiphertextMessage.prekeyType) {
    await remoteSessionCipher
        .decryptWithCallback(ciphertext as PreKeySignalMessage, (plaintext) {
      // ignore: avoid_print
      print(utf8.decode(plaintext));
    });
  }
}

Future<void> groupSession() async {
  final senderKeyName = SenderKeyName('', SignalProtocolAddress('sender', 1));
  final senderKeyStore = InMemorySenderKeyStore();
  final groupSession = GroupCipher(senderKeyStore, senderKeyName);
  await groupSession.encrypt(Uint8List.fromList(utf8.encode('Hello Mixin')));
}
56
likes
0
points
231
downloads

Publisher

verified publishermixin.dev

Weekly Downloads

Signal Protocol libray for Dart native and Flutter, pure Dart implementation of the the Signal Protocol

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

collection, convert, crypto, ed25519_edwards, fixnum, optional, pointycastle, protobuf, tuple, very_good_analysis, x25519

More

Packages that depend on libsignal_protocol_dart