How to hash SHA-256 in Dart
In this Dart tutorial we show you how to use the crypto library in Dart to perform SHA-256 hashing.
Install crypto package in Dart or Flutter
To use the crypto package with Dart, run the following command.
dart pub add cryptoTo use the crypto package with Flutter, run the following command.
flutter pub add cryptoThe package can be added to your pubspec.yaml as below.
dependencies:
crypto: ^3.0.1How to hash SHA-256 in Dart
In the following Dart program, we use the crypto package to perform SHA-256 hashing which input as a string and return hexadecimal digits string as a result of hashing function.
import 'package:crypto/crypto.dart';
import 'dart:convert';
void main() {
var dataToHash = 'toricode';
var bytesToHash = utf8.encode(dataToHash);
var sha256Digest = sha256.convert(bytesToHash);
print('Data to hash: $dataToHash');
print('SHA-256: $sha256Digest');
}Data to hash: toricode
SHA-256: d194e39601b8efeb347b0dbe330427aacb5e508e6a07972c1687fef2a5c9b6d0Happy Coding 😊