搜索插件、代码仓与 APP 分类

插件详情v4.3.1

crypt

使用 Unix crypt 格式对加盐密码进行单向字符串哈希

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
9230
喜欢数
38
Pub 分数
160

安装插件

flutter pub add crypt

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

crypt 包提供了一种单向字符串哈希功能,用于以 Unix crypt 格式安全地存储带盐密码。它使开发人员能够以与传统 Unix 系统兼容的方式对密码进行哈希处理,通过加盐和加密哈希确保强大的安全性。该包设计用于跨平台使用,支持 Android、iOS、Web、macOS、Windows 和 Linux。它可无缝集成到需要安全密码处理的 Flutter 应用中,而无需暴露原始凭据。

适合场景

  • 安全密码存储
  • 用户身份验证系统
  • 跨平台密码哈希
  • 旧系统兼容性
  • 密码验证工作流

主要特点

  • Unix crypt 格式支持
  • 加盐密码哈希
  • 跨平台兼容性
  • 简单 API 集成
  • 安全的单向哈希

适合对象

  • 需要安全密码管理的应用程序
  • 使用基于Unix的身份验证的开发人员
  • 需要与旧系统互操作的项目
  • 注重安全的应用程序
  • 带有用户登录功能的 Flutter 应用

使用建议

  • 不适合用于通用哈希
  • 需要了解 crypt 格式限制
  • 可能不符合现代密码哈希标准(如 bcrypt 或 scrypt)的要求
  • 仅限密码相关使用场景
  • 取决于底层系统的加密实现

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

crypt

One-way string hashing for salted passwords using the Unix crypt format.

This package implements the SHA-256 crypt hash and SHA-512 crypt hash, cryptographic hash formats that are specified in "Unix crypt using SHA-256 and SHA-512" (version: 0.6 2016-08-31).

These hashes are commonly used in Unix and POSIX systems, and in LDAP entries as authentication credentials for POSIX accounts.

Crypt format strings

It can produce crypt formatted string like:

$5$xYWYo0raYwLSchAd$na8cL1H.ESWtof6DNwraE6p8WI9DYObZ3irMe01Guk6

and

$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBAwqFMz2.opqey6IcA

The format is a sequence of fields, leading and separated by a dollar sign ($):

  • algorithm indicator;
  • number of rounds (optional if it is the default number of rounds);
  • the salt; and
  • the hash value.

The formated string starts with $5$ for a SHA-256 crypt and $6$ for a SHA-512 crypt. There are other (less secure) algorithms, but they are currently not supported by this package.

When SHA-256 or SHA-512 is being used, the default number of rounds is 5000 (as defined by the specification).

Note: different systems use the crypt formatted string differently. For example, as the value of the userPassword attribute in an LDAP posixAccount entry, {crypt} needs to be prepended to it. For example,

{crypt}$5$xYWYo0raYwLSchAd$na8cL1H.ESWtof6DNwraE6p8WI9DYObZ3irMe01Guk6

Usage

Checking against a crypt format hash

To test if an entered password matches the password that was used to create a crypt format hash, create a Crypt object from the crypt format has and invoke the match method on it.


bool isValid(String cryptFormatHash, String enteredPassword) =>
  Crypt(cryptFormatHash).match(enteredPassword);
Generating a crypt format hash

To generate a crypt format hash, use Crypt.sha256 or Crypt.sha512 constructors, and convert it to a String.

import 'package:crypt/crypt.dart';

void main() {
  // Creating crypt strings

  // Default rounds and random salt generated
  final c1 = Crypt.sha256('p@ssw0rd');

  // Random salt generated
  final c2 = Crypt.sha256('p@ssw0rd', rounds: 10000);

  // Default rounds
  final c3 = Crypt.sha256('p@ssw0rd', salt: 'abcdefghijklmnop');

  // No defaults used
  final c4 = Crypt.sha256('p@ssw0rd', rounds: 10000,
                          salt: 'abcdefghijklmnop');

  // SHA-512
  final d1 = Crypt.sha512('p@ssw0rd');

  print(c1);
  print(c2);
  print(c3);
  print(c4);
  print(d1);

  // Comparing a value to a crypt hash

  for (final hashString in [
    r'$5$zQUCjEzs9jnrRdCK$dbo1i9WjQjbUwOC4JCRAZHpfd31Dh676vI0L6w0dZw1',
    c1.toString(),
    c2.toString(),
    c3.toString(),
    c4.toString(),
    d1.toString(),
  ]) {
    // Parse the crypt string: this extracts the type, rounds and salt
    final h = Crypt(hashString);

    const correctValue = 'p@ssw0rd';
    const wrongValue = '123456';

    if (!h.match(correctValue)) {
      print('Error: unexpected non-match: $correctValue');
    }

    if (h.match(wrongValue)) {
      print('Error: unexpected match: $wrongValue');
    }
  }
}

The above example produced the following output:

$5$jYq8PvB6hI3cLREQ$FGBjCL5NO1qSwync3LOlCWTnIBJCjVsFtst9jNnnBx9
$5$rounds=10000$wJiiNy1TwwaWhGFN$t2JsIqOgfXh/3LLQF.YA9XDlJmtpLYmSe4i9TZl7cM.
$5$abcdefghijklmnop$gUWLu9sDI2Qvs112Xb8jmgD3ySIRE5ek63jk6ybSs7D
$5$rounds=10000$abcdefghijklmnop$51muKIziT9VAyDZ2ZueAYvAwgIYx0cLxUCIAlPoWaHD
$6$LJgzW1oI9UZ5w8HO$pTL3hmFg2zBkQPqRhcej6CmY2Az0WLDVlnMGTg//71D3hDEvKCB7XqwtinHEM1rlD/YAlEjhy2Lb3LJQsNvXx.

Features and bugs

Random number generators

Salt generation uses a cryptographically secure random number generator, if one is available. If one is not available, it falls back to using a cryptographically insecure one.

Set Crypt.cryptographicallySecureSalts to true to prevent a cryptographically insecure random number from being used. An exception will then be thrown if attempting to generate a salt on platforms that don't support a cryptographically secure random number generator.

Explicitly set it to false to allow this fallback behaviour in future releases. The default is currently set to false, for backward compatibility. But a future release may set the default to true for improved security. Explicitly setting it to false will ensure code will still work when that breaking change is made.

Dependency on the crypto package

The current release depends on the Dart crypto package version 3.0.0, which has support for SHA-512. If you need to use an older version of crypto, use version 2.0.0 of this package -- but that older version won't have support for SHA-512 crypt strings and is not null safe.

Please file feature requests and bugs at the GitHub issue tracker.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目