Skip to content

Commit ff6d787

Browse files
committed
[common_crypto] add hmac sha256 calculator
1 parent 1bf4315 commit ff6d787

File tree

4 files changed

+80
-10
lines changed

4 files changed

+80
-10
lines changed

packages/common_crypto/lib/src/aes.dart

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@ import 'package:ffi/ffi.dart';
66
import 'common_crypto_bindings_generated.dart';
77
import 'tools.dart';
88

9-
extension _Uint8ListPointer on Uint8List {
10-
Pointer<Uint8> get pointer {
11-
final pointer = malloc<Uint8>(length);
12-
for (var i = 0; i < length; i++) {
13-
pointer[i] = this[i];
14-
}
15-
return pointer;
16-
}
17-
}
18-
199
Uint8List _aesCrypt({
2010
required Uint8List key,
2111
required Uint8List data,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'dart:ffi';
2+
import 'dart:typed_data';
3+
4+
import 'package:ffi/ffi.dart';
5+
6+
import 'common_crypto_bindings_generated.dart';
7+
import 'tools.dart';
8+
9+
const _kDigestDataCount = 32;
10+
11+
class HMacSha256 implements Finalizable {
12+
static Uint8List hmacSha256({
13+
required Uint8List key,
14+
required Uint8List data,
15+
}) {
16+
final hmac = HMacSha256(key);
17+
hmac.update(data);
18+
final result = hmac.finalize();
19+
hmac.dispose();
20+
return result;
21+
}
22+
23+
final Uint8List key;
24+
25+
final context = malloc<CCHmacContext>();
26+
27+
HMacSha256(this.key) {
28+
final keyRef = key.pointer;
29+
bindings.CCHmacInit(context, kCCHmacAlgSHA256, keyRef.cast(), key.length);
30+
malloc.free(keyRef);
31+
}
32+
33+
void update(Uint8List data) {
34+
final dataRef = data.pointer;
35+
bindings.CCHmacUpdate(context, dataRef.cast(), data.length);
36+
malloc.free(dataRef);
37+
}
38+
39+
Uint8List finalize() {
40+
final digest = malloc<Uint8>(_kDigestDataCount);
41+
bindings.CCHmacFinal(context, digest.cast());
42+
final result = Uint8List.fromList(digest.asTypedList(_kDigestDataCount));
43+
malloc.free(digest);
44+
return result;
45+
}
46+
47+
void dispose() {
48+
malloc.free(context);
49+
}
50+
}

packages/common_crypto/lib/src/tools.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import 'dart:ffi';
22
import 'dart:io';
3+
import 'dart:typed_data';
4+
5+
import 'package:ffi/ffi.dart';
36

47
import 'common_crypto_bindings_generated.dart';
58

@@ -12,3 +15,13 @@ final DynamicLibrary _dylib = () {
1215

1316
/// The bindings to the native functions in [_dylib].
1417
final CommonCryptoBindings bindings = CommonCryptoBindings(_dylib);
18+
19+
extension Uint8ListPointer on Uint8List {
20+
Pointer<Uint8> get pointer {
21+
final pointer = malloc<Uint8>(length);
22+
for (var i = 0; i < length; i++) {
23+
pointer[i] = this[i];
24+
}
25+
return pointer;
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'dart:convert';
2+
import 'dart:typed_data';
3+
4+
import 'package:common_crypto/src/hmac.dart';
5+
import 'package:flutter_test/flutter_test.dart';
6+
7+
void main() {
8+
test('hmac 256 test', () {
9+
final key = base64Decode('Y9hRHsqr9adyX29DYsjWhg==');
10+
final data = Uint8List.fromList(utf8.encode('Mixin'));
11+
final result = HMacSha256.hmacSha256(key: key, data: data);
12+
expect(
13+
base64Encode(result),
14+
equals('Zg/Z5GkXYHKPR/uPVOe4Z5ZPzSgRoDL72mrm5/TyCrQ='),
15+
);
16+
});
17+
}

0 commit comments

Comments
 (0)