Git Product home page Git Product logo

crclib.dart's Introduction

Dart crclib

This is not an official Google product.

Generic CRC calculations as Dart converters and some common algorithms.

To calculate the CRC value of any message

The easiest way to use this library is to call convert on the instance of the desired CRC routine.

  Crc32Xz().convert(utf8.encode('123456789')) == 0xCBF43926

Another supported use case is as stream transformers.

  File(...).openRead().transform(Crc32Xz()).single.then(...)

Instead of using predefined classes, it is also possible to construct a customized CRC function with ParametricCrc class. For a list of known CRC routines, check out https://reveng.sourceforge.io/crc-catalogue/all.htm.

TODO:

  1. inputReflected and outputReflected can be different, see CRC-12/UMTS.
  2. Bit-level checksums (including non-multiple-of-8 checksums).

To flip bits to obtain desired CRC values

With CrcFlipper, one can call flipWithData or flipWithValue depending on whether they have access to the message, or only its calculated CRC value. The snippet below is taken from test/flipper_test.dart.

      var inputMessage =
          'flipping lowercases to uppercases like mama pig making hot pancakes '
          'for daddy pig in peppa pig cartoon';
      // Mark the lower/upper-case bit in each character.
      // 0x61 = 'a' = 0110 0001
      //                | <-- 5th bit, zero-indexed
      // 0x41 = 'A' = 0100 0001
      var positions = inputMessage.codeUnits
          .asMap()
          .entries
          .where((e) => e.value >= 0x61 && e.value < 0x61 + 26)
          .map((e) => e.key * 8 + 5)
          .toSet();
      var flipper = CrcFlipper(Crc64());
      var solution = flipper.flipWithData(inputMessage.codeUnits, positions,
          CrcValue(BigInt.parse('DEADBEEFCAFEBABE', radix: 16)))!;
      var tmp = List.of(inputMessage.codeUnits, growable: false);
      solution.forEach((bitPosition) {
        var mask = 1 << (bitPosition % 8);
        tmp[bitPosition ~/ 8] ^= mask;
      });
      var outputMessage = String.fromCharCodes(tmp);
      expect(
          outputMessage,
          'flIPpiNG LOWErcAsEs To uPpERcaseS LIkE mAmA Pig mAKInG hOT paNcAKEs '
          'For DAdDY pig in peppa pig cartoon');

The detailed algorithm is documented in flipping_algorithm.md.

crclib.dart's People

Contributors

davidmorgan avatar postmasters avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

crclib.dart's Issues

BadState with null safety version

I'm trying to run with the nullsaftey version but I'm getting bad state error when I call single.

Future<T> get first {
    _Future<T> future = new _Future<T>();
    StreamSubscription<T> subscription =
        this.listen(null, onError: future._completeError, onDone: () {
      try {
throw IterableElementError.noElement();

StateError (Bad state: No element)

This is my code:

import 'dart:io';

import 'package:crclib/catalog.dart';
import 'package:dcli/dcli.dart';
import 'package:test/test.dart';

void main() {
  
  test('hash performance', () async {
    withTempFile((largeFile) async {
      try {
        createLargeFile(largeFile);
        now();
        await File(largeFile).openRead().transform(Crc32()).single;
        now();
      } catch (e) {
        print(e);
      }
    });
  });
}

void now() {
  print(DateTime.now());
}

void createLargeFile(String largeFile) {
  for (int i = 0; i < 100000; i++) {
    largeFile.append('*' * 1000);
  }
}
name: batman
version: 1.0.2
homepage: https://github.com/noojee/batman
description: A System Integrity Monitor for PCI DSS compliance. batman provides file integrity monitoring and log scanners with a built in scheduler suitable for use in a docker container.
repository: https://github.com/noojee/batman
environment: 
  sdk: '>=2.14.4 <3.0.0'
dependencies: 
  dcli: ^1.13.0
  dcli_core: ^0.0.2
  crclib:
    git:
      url: https://github.com/google/crclib.dart.git
      ref: nullsafety  

Does this package have Python crc_hqx function equivalent?

I am trying to convert Python library to dart and I am not sure what the equivalent function for crc_hqx in this dependency.

Example from python

binary_packet = 'fef052000232a1000000000034000100000000000000000013bb2e6100000000000000000000f0fe1c00000000000000000000000000000000000000000000000000000000000000000000000000'
or
binary_packet = [254, 240, 82, 0, 2, 50, 161, 0, 0, 0, 0, 0, 52, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 168, 187, 46, 97, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 254, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

result = crc_hqx(binary_packet, 0x1021);
// result will be 56902

calculated CRC16 ARC

can I calculate the CRC16 ARC with this package
I have a data [0x0E 0x20 0xE0 0x07 0xA5 0x03 0x00 0x00 0x2E 0x46 0x49 0x54] and I want to calculate the CRC16
when I insert the data on the CRC Calculator the result is 0xA447
thanks..

package:crclib/catalog.dart not found

Hi.
This problem occurs on iOS and Android platforms.

The Flutter environment:

[✓] Flutter (Channel dev, 1.27.0-4.0.pre, on macOS 11.1 20C69 darwin-x64, locale en-GB)
    • Flutter version 1.27.0-4.0.pre at /Users/sissa/flutter
    • Framework revision f8cd24de95 (9 weeks ago), 2021-02-16 11:24:17 -0800
    • Engine revision 1d537824d6
    • Dart version 2.13.0 (build 2.13.0-30.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/sissa/Library/Android/sdk
    • Platform android-30, build-tools 29.0.2
    • ANDROID_HOME = /Users/sissa/Library/Android/sdk
    • Java binary at: /Applications/Android Studio 2.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.4, Build version 12D4e
    • CocoaPods version 1.10.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 3.5)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] Android Studio (version 4.1)
    • Android Studio at /Applications/Android Studio 2.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)

[✓] VS Code (version 1.40.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (3 available)
    • PRA LX1 (mobile)         • 9DC7N17707001642                         • android-arm64  • Android 7.0 (API 24)
    • SAMCLA’s iPhone (mobile) • f063d3c4cfd212cac4386163446403a522fa40cb • ios            • iOS 14.4.2
    • Chrome (web)             • chrome                                   • web-javascript • Google Chrome 89.0.4389.128

• No issues found!

My pubspec.yaml dependencies:

environment:
  sdk: ">=2.7.0 <3.0.0"
  flutter: ">=1.17.0"

dependencies:
  crclib: ^2.0.0
  flutter:
    sdk: flutter

  flutter_blue:
    git:
      url: git://github.com/pauldemarco/flutter_blue.git

  webdav_client: ^1.0.0

dependency_overrides:
  intl: ^0.17.0-nullsafety.2

dev_dependencies:
  flutter_test:
    sdk: flutter

  mockito: ^5.0.5
  test:

publish_to: none

When I try to build the app, throw this error:

Failed to build iOS app
Error output from Xcode build:
↳
    ** BUILD FAILED **


Xcode's output:
↳
    Error: Could not resolve the package 'crclib' in 'package:crclib/catalog.dart'.
    ../ota_package/lib/ota/utils/download_manager/download_manager_impl.dart:1:8: Error: Not found: 'package:crclib/catalog.dart'
    import 'package:crclib/catalog.dart';
           ^
    ../ota_package/lib/ota/utils/download_manager/download_manager_impl.dart:69:24: Error: The method 'Crc32' isn't defined for the class 'DownloadManagerImpl'.
     - 'DownloadManagerImpl' is from 'package:ota_package/ota/utils/download_manager/download_manager_impl.dart' ('../ota_package/lib/ota/utils/download_manager/download_manager_impl.dart').
    Try correcting the name to the name of an existing method, or defining a method named 'Crc32'.
        String crcResult = Crc32().convert(otaDownloadedFile.getRange(0, otaDownloadedFile.length - 4).toList()).toRadixString(16);
                           ^^^^^
    Unhandled exception:
    FileSystemException(uri=org-dartlang-untranslatable-uri:package%3Acrclib%2Fcatalog.dart; message=StandardFileSystem only supports file:* and data:* URIs)
    #0      StandardFileSystem.entityForUri (package:front_end/src/api_prototype/standard_file_system.dart:33:7)
    #1      asFileUri (package:vm/kernel_front_end.dart:599:37)
    #2      writeDepfile (package:vm/kernel_front_end.dart:738:21)
    <asynchronous suspension>
    #3      FrontendCompiler.compile (package:frontend_server/frontend_server.dart:558:9)
    <asynchronous suspension>
    #4      starter (package:flutter_frontend_server/server.dart:180:12)
    <asynchronous suspension>
    #5      main (file:///opt/s/w/ir/cache/builder/src/flutter/flutter_frontend_server/bin/starter.dart:13:24)
    <asynchronous suspension>


    Command PhaseScriptExecution failed with a nonzero exit code
    note: Using new build system
    note: Building targets in parallel
    note: Planning build
    note: Constructing build description

Could not build the precompiled application for the device.

Error launching application on SAMCLA’s iPhone.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.