twonly-app-dependencies/hashlib/lib/src/crc16.dart
2025-12-07 16:10:41 +01:00

52 lines
1.5 KiB
Dart

// Copyright (c) 2023, Sudipto Chandra
// All rights reserved. Check LICENSE file for details.
import 'dart:convert';
import 'package:hashlib/src/algorithms/crc/crc16.dart';
import 'package:hashlib/src/core/hash_base.dart';
export 'package:hashlib/src/algorithms/crc/crc16.dart'
show CRC16Hash, CRC16Params;
/// A CRC or cyclic redundancy check is code commonly used for error detection
/// and correction of digital data. This generates a 16-bit number as output.
///
/// **WARNING: It should not be used for cryptographic purposes.**
const crc16 = CRC16(CRC16Params.ibm);
/// CRC-16 code generator
class CRC16 extends HashBase {
final CRC16Params params;
/// Create a instance for generating CRC-16 hashes
const CRC16(this.params);
@override
String get name => "CRC-16/${params.name}";
@override
CRC16Hash createSink() =>
params.reversed ? CRC16ReverseHash(params) : CRC16NormalHash(params);
/// Gets the CRC-16 code for a String
///
/// Parameters:
/// - [input] is the string to hash
/// - The [encoding] is the encoding to use. Default is `input.codeUnits`
int code(String input, [Encoding? encoding]) =>
string(input, encoding).number();
}
/// Gets the CRC-16 value.
///
/// Parameters:
/// - [input] is the string to hash
/// - The [encoding] is the encoding to use. Default is `input.codeUnits`
/// - The [params] is the parameters to use. Default: [CRC16Params.ansi]
int crc16code(
String input, {
Encoding? encoding,
CRC16Params params = CRC16Params.ansi,
}) =>
CRC16(params).code(input, encoding);