How to Hash SHA-1 in Dart
In this Dart tutorial we learn how to perform SHA-1 hashing in Dart using the crypto library.
Install crypto package in Dart or Flutter
To use the crypto package with Dart, run the following command.
dart pub add crypto
To use the crypto package with Flutter, run the following command.
flutter pub add crypto
The package can be added to your pubspec.yaml as below.
dependencies:
crypto: ^3.0.1
How to hash SHA-1 in Dart
In the following Dart program, we use the crypto package to perform SHA-1 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 digest = sha1.convert(bytesToHash);
print('Data to hash: $dataToHash');
print('SHA-1: $digest');
}
Data to hash: ToriCode
SHA-1: 45a6b75826dc9e7c88a8a7c6ac0ee3ba335cc9ba
Happy Coding 😊