-
Notifications
You must be signed in to change notification settings - Fork 53
/
common.dart
114 lines (94 loc) · 2.93 KB
/
common.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import 'dart:io';
import 'dart:async';
import 'dart:convert';
import 'package:hex/hex.dart';
import 'package:stash/stash_api.dart';
import 'package:stash_hive/stash_hive.dart';
import 'package:crypto/crypto.dart';
class Common {
const Common();
// Temporary directory
static final path = Directory.systemTemp.path;
// Creates a store
static final store = newHiveDefaultVaultStore(path: path);
// Creates a vault from the previously created store
static final vault = store.vault<String>(
name: 'cookieVault',
eventListenerMode: EventListenerMode.synchronous,
);
/// Add / Replace this [Vault] [value] for the specified [key].
///
/// * [key]: the key
/// * [value]: the value
static Future<void> storageSet(String key, String value) async {
await vault.put(key, value);
}
/// Returns the stash value for the specified [key]
///
/// * [key]: the key
///
/// Returns a [String]
static Future<String?> storageGet(String key) async {
return await vault.get(key);
}
/// Removes the mapping for a [key] from this [Vault] if it is present.
///
/// * [key]: key whose mapping is to be removed from the [Vault]
///
/// Returns `true` if the removal of the mapping for the specified [key] was successful.
static Future<bool> storageRemove(String key) async {
await vault.remove(key);
return !await vault.containsKey(key);
}
static bool equalsIgnoreCase(String? string1, String? string2) {
return string1?.toLowerCase() == string2?.toLowerCase();
}
static String toJson(dynamic object) {
var encoder = JsonEncoder.withIndent(' ');
return encoder.convert(object);
}
static dynamic fromJson(String? jsonString) {
if (jsonString == null) {
return null;
}
return json.decode(jsonString);
}
static bool hasKeyIgnoreCase(Map map, String key) {
return map.keys.any((x) => equalsIgnoreCase(x, key));
}
static String toHexString(List<int> data) {
return HEX.encode(data);
}
static List fromHexString(String hexString) {
return HEX.decode(hexString);
}
static String hashStringSHA256(String input) {
var bytes = utf8.encode(input);
var digest = sha256.convert(bytes);
return toHexString(digest.bytes);
}
static String encodeMap(Map data) {
return data.keys.map((key) {
var k = Uri.encodeComponent(key.toString());
var v = Uri.encodeComponent(data[key].toString());
return '$k=$v';
}).join('&');
}
static List<String> split(String string, String separator, {int max = 0}) {
var result = <String>[];
if (separator.isEmpty) {
result.add(string);
return result;
}
while (true) {
var index = string.indexOf(separator, 0);
if (index == -1 || (max > 0 && result.length >= max)) {
result.add(string);
break;
}
result.add(string.substring(0, index));
string = string.substring(index + separator.length);
}
return result;
}
}