Skip to content

Commit e25ac3e

Browse files
committed
add dargon2_interface
1 parent 43efeea commit e25ac3e

15 files changed

+855
-0
lines changed

dargon2_interface/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## 1.0.1
2+
- Bump down Dart version to 2.14 so Flutter 2.5 can use
3+
4+
## 1.0.0
5+
6+
- Initial version.
7+
- Interface from dargon2_core to allow federated plugins (needed for web support)

dargon2_interface/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Tejas Mehta
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

dargon2_interface/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## dargon2_interface
2+
3+
This library generally should not be used in most contexts
4+
5+
This library only provides an interface to implement a dargon2-compatible argon2 hash binding.
6+
7+
This library is used by `dargon2_core` and `dargon2_flutter_web` to maintain cross-platform hashing functionality.
8+
9+
## Usage
10+
11+
A simple usage example:
12+
13+
```dart
14+
import 'package:dargon2_interface/dargon2_interface.dart';
15+
16+
void main() {
17+
// Create an instance of TestDArgon2
18+
var dargon2 = TestDArgon2();
19+
}
20+
21+
class TestDArgon2 extends DArgon2 {
22+
@override
23+
Future<DArgon2Result> hashPasswordBytes(List<int> password, {required Salt salt, int iterations = 32, int memory = 256, int parallelism = 2, int length = 32, Argon2Type type = Argon2Type.i, Argon2Version version = Argon2Version.V13}) {
24+
// Create an implementation for hashing passwords with the given parameters
25+
throw UnimplementedError();
26+
}
27+
28+
@override
29+
Future<bool> verifyHashBytes(List<int> password, List<int> encodedHash, {Argon2Type type = Argon2Type.i}) {
30+
// Create an implementation for verifying passwords with the given parameters
31+
throw UnimplementedError();
32+
}
33+
}
34+
```
35+
36+
## Features and bugs
37+
38+
Please file feature requests and bugs at the [issue tracker].
39+
40+
[issue tracker]: https://github.com/tmthecoder/dargon2_interface/issues
41+
42+
## Licensing
43+
44+
- dargon2_core is Licensed under the [MIT License]
45+
- The C implementation of [Argon2] is licensed under a dual [Apache and CC0 License]
46+
47+
[MIT License]: https://github.com/tmthecoder/dargon2_interface/blob/main/LICENSE
48+
49+
[Argon2]: https://github.com/P-H-C/phc-winner-argon2
50+
51+
[Apache and CC0 License]: https://github.com/P-H-C/phc-winner-argon2/blob/master/LICENSE
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file configures the static analysis results for your project (errors,
2+
# warnings, and lints).
3+
#
4+
# This enables the 'recommended' set of lints from `package:lints`.
5+
# This set helps identify many issues that may lead to problems when running
6+
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7+
# style and format.
8+
#
9+
# If you want a smaller set of lints you can change this to specify
10+
# 'package:lints/core.yaml'. These are just the most critical lints
11+
# (the recommended set includes the core lints).
12+
# The core lints are also what is used by pub.dev for scoring packages.
13+
14+
include: package:lints/recommended.yaml
15+
16+
# Uncomment the following section to specify additional rules.
17+
18+
# linter:
19+
# rules:
20+
# - camel_case_types
21+
22+
# analyzer:
23+
# exclude:
24+
# - path/to/excluded/files/**
25+
26+
# For more information about the core and recommended set of lints, see
27+
# https://dart.dev/go/core-lints
28+
29+
# For additional information about configuring this file, see
30+
# https://dart.dev/guides/language/analysis-options
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="WEB_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<excludeFolder url="file://$MODULE_DIR$/build" />
7+
<excludeFolder url="file://$MODULE_DIR$/.pub" />
8+
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
9+
</content>
10+
<orderEntry type="sourceFolder" forTests="false" />
11+
<orderEntry type="library" name="Dart SDK" level="project" />
12+
<orderEntry type="library" name="Dart Packages" level="project" />
13+
</component>
14+
</module>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'package:dargon2_interface/dargon2_interface.dart';
2+
3+
void main() {
4+
// Create an instance of TestDArgon2
5+
var dargon2 = TestDArgon2();
6+
}
7+
8+
class TestDArgon2 extends DArgon2 {
9+
@override
10+
Future<DArgon2Result> hashPasswordBytes(List<int> password, {required Salt salt, int iterations = 32, int memory = 256, int parallelism = 2, int length = 32, Argon2Type type = Argon2Type.i, Argon2Version version = Argon2Version.V13}) {
11+
// Create an implementation for hashing passwords with the given parameters
12+
throw UnimplementedError();
13+
}
14+
15+
@override
16+
Future<bool> verifyHashBytes(List<int> password, List<int> encodedHash, {Argon2Type type = Argon2Type.i}) {
17+
// Create an implementation for verifying passwords with the given parameters
18+
throw UnimplementedError();
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// Support for doing something awesome.
2+
///
3+
/// More dartdocs go here.
4+
library dargon2_interface;
5+
6+
export 'src/argon2.dart';
7+
export 'src/dargon2_error_codes.dart';
8+
export 'src/dargon2_exception.dart';
9+
export 'src/dargon2_result.dart';
10+
export 'src/salt.dart';

dargon2_interface/lib/src/argon2.dart

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright 2020 Tejas Mehta <[email protected]>
2+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3+
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
4+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5+
6+
import 'dart:convert';
7+
import 'dargon2_result.dart';
8+
9+
import 'salt.dart';
10+
11+
/// The enum to determine the Argon2 Type used (Argon2i, Argon2d, Argon2id).
12+
enum Argon2Type {
13+
/// Utilize Argon2d for hashing
14+
d,
15+
16+
/// Utilize Argon2i for hashing
17+
i,
18+
19+
/// Utilize Argon2id for hashing
20+
id
21+
}
22+
23+
/// The enum used to determine the Argon2 Version used (0x10 or 0x13).
24+
enum Argon2Version {
25+
/// Utilize Argon2 V10 for hashing
26+
V10,
27+
28+
/// Utilize Argon2 V13 for hashing
29+
V13
30+
}
31+
32+
/// A class that houses all of the methods to hash and verify a password using
33+
/// the [Argon2] password hashing algorithm.
34+
///
35+
/// [Argon2]: https://github.com/P-H-C/phc-winner-argon2
36+
///
37+
/// This should be used via the [argon2] field.
38+
abstract class DArgon2 {
39+
/// The Future method to hash a [String] password with Argon2
40+
///
41+
/// Needs a UTF-8 String [password] and a [salt] to be given with
42+
/// an optional parameters to control the amount of [iterations], [memory],
43+
/// [parallelism] used during the operation. Also optionally takes a [length]
44+
/// parameter for the hash's return length, as well as a [type] and [version].
45+
///
46+
/// Returns a [Future] containing a [DArgon2Result] with the hashed password,
47+
/// encoded hash, and various conversion options for the hash and encoded bytes.
48+
Future<DArgon2Result> hashPasswordString(String password,
49+
{required Salt salt,
50+
int iterations = 32,
51+
int memory = 256,
52+
int parallelism = 2,
53+
int length = 32,
54+
Argon2Type type = Argon2Type.i,
55+
Argon2Version version = Argon2Version.V13}) =>
56+
hashPasswordBytes(utf8.encode(password),
57+
salt: salt,
58+
iterations: iterations,
59+
memory: memory,
60+
parallelism: parallelism,
61+
length: length,
62+
type: type,
63+
version: version);
64+
65+
/// The Future method to hash a List<int> password with Argon2
66+
///
67+
/// Needs a [List] of type int [password] and a [salt] to be given with
68+
/// an optional parameters to control the amount of [iterations], [memory],
69+
/// [parallelism] used during the operation. Also optionally takes a [length]
70+
/// parameter for the hash's return length, as well as a [type] and [version].
71+
///
72+
/// Returns a [Future] containing a [DArgon2Result] with the hashed password,
73+
/// encoded hash, and various conversion options for the hash and encoded bytes.
74+
Future<DArgon2Result> hashPasswordBytes(List<int> password,
75+
{required Salt salt,
76+
int iterations = 32,
77+
int memory = 256,
78+
int parallelism = 2,
79+
int length = 32,
80+
Argon2Type type = Argon2Type.i,
81+
Argon2Version version = Argon2Version.V13});
82+
83+
/// The Future method to handle verifying a String argon2 hash against a String password
84+
///
85+
/// Needs a UTF-8 String [password], a UTF-8 String [encodedHash], as well as an
86+
/// Argon2Type [type] used for the actual hash. This method gets the byte arrays of
87+
/// both Strings and calls [verifyHashBytes] with that info.
88+
///
89+
/// Returns a [Future] with the [bool] of whether the hashing was a match or not
90+
Future<bool> verifyHashString(String password, String encodedHash,
91+
{Argon2Type type = Argon2Type.i}) =>
92+
verifyHashBytes(utf8.encode(password), utf8.encode(encodedHash),
93+
type: type);
94+
95+
/// The Future method to handle verifying a List<int> argon2 hash against a List<int> password
96+
///
97+
/// Needs a [List] of type int [password], a [List] of type int [encodedHash], as well as an
98+
/// Argon2Type [type] used for the actual hash.
99+
///
100+
/// Returns a [Future] with the [bool] of whether the hashing was a match or not
101+
Future<bool> verifyHashBytes(List<int> password, List<int> encodedHash,
102+
{Argon2Type type = Argon2Type.i});
103+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright 2020 Tejas Mehta <[email protected]>
2+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3+
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
4+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5+
6+
/// The enum with all of the Argon2 Error codes, seen directly from the C
7+
/// Reference Library of Argon2. The codes are sent into the Exception object as well
8+
/// as used to retrieve the error message from the C library.
9+
enum DArgon2ErrorCode {
10+
/// No error
11+
ARGON2_OK,
12+
13+
/// Outpointer null
14+
ARGON2_OUTPUT_PTR_NULL,
15+
16+
/// Output pointer too short
17+
ARGON2_OUTPUT_TOO_SHORT,
18+
19+
/// Output pointer too long
20+
ARGON2_OUTPUT_TOO_LONG,
21+
22+
/// Password too short
23+
ARGON2_PWD_TOO_SHORT,
24+
25+
/// Password too long
26+
ARGON2_PWD_TOO_LONG,
27+
28+
/// Salt too short
29+
ARGON2_SALT_TOO_SHORT,
30+
31+
/// Salt too long
32+
ARGON2_SALT_TOO_LONG,
33+
34+
/// Associated Data too short
35+
ARGON2_AD_TOO_SHORT,
36+
37+
/// Associated Data too long
38+
ARGON2_AD_TOO_LONG,
39+
40+
/// Secret too short
41+
ARGON2_SECRET_TOO_SHORT,
42+
43+
/// Secret too long
44+
ARGON2_SECRET_TOO_LONG,
45+
46+
/// Time too short
47+
ARGON2_TIME_TOO_SMALL,
48+
49+
/// Time too long
50+
ARGON2_TIME_TOO_LARGE,
51+
52+
/// Memory too small
53+
ARGON2_MEMORY_TOO_LITTLE,
54+
55+
/// Memory too large
56+
ARGON2_MEMORY_TOO_MUCH,
57+
58+
/// Too few lanes
59+
ARGON2_LANES_TOO_FEW,
60+
61+
/// Too many lanes
62+
ARGON2_LANES_TOO_MANY,
63+
64+
/// Null password pointer with a given nonzero length
65+
ARGON2_PWD_PTR_MISMATCH,
66+
67+
/// Null salt pointer with a given nonzero length
68+
ARGON2_SALT_PTR_MISMATCH,
69+
70+
/// Null secret pointer with a given nonzero length
71+
ARGON2_SECRET_PTR_MISMATCH,
72+
73+
/// Null associated data pointer with a given nonzero length
74+
ARGON2_AD_PTR_MISMATCH,
75+
76+
/// Memory alloc error
77+
ARGON2_MEMORY_ALLOCATION_ERROR,
78+
79+
/// Null free memory call back
80+
ARGON2_FREE_MEMORY_CBK_NULL,
81+
82+
/// Null allocation callback
83+
ARGON2_ALLOCATE_MEMORY_CBK_NULL,
84+
85+
/// Context is null
86+
ARGON2_INCORRECT_PARAMETER,
87+
88+
/// No such type of Argon2
89+
ARGON2_INCORRECT_TYPE,
90+
91+
/// Output pointer mismatch
92+
ARGON2_OUT_PTR_MISMATCH,
93+
94+
/// Too few threads
95+
ARGON2_THREADS_TOO_FEW,
96+
97+
/// Too many threads
98+
ARGON2_THREADS_TOO_MANY,
99+
100+
/// Missing arguments
101+
ARGON2_MISSING_ARGS,
102+
103+
/// Encoding failed
104+
ARGON2_ENCODING_FAIL,
105+
106+
/// Decoding failed
107+
ARGON2_DECODING_FAIL,
108+
109+
/// Threading failed
110+
ARGON2_THREAD_FAIL,
111+
112+
/// Some parameters are too short/long
113+
ARGON2_DECODING_LENGTH_FAIL,
114+
115+
/// Verification mismatch
116+
ARGON2_VERIFY_MISMATCH,
117+
118+
/// Unknown error
119+
ARGON2_UNKNOWN_ERROR
120+
}

0 commit comments

Comments
 (0)