40 lines
1.4 KiB
Dart
40 lines
1.4 KiB
Dart
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:common_utils/common_utils.dart';
|
|
import 'package:pointycastle/export.dart';
|
|
|
|
import 'package:pj_supermarket_cloud/common/index.dart';
|
|
|
|
class EncryptionTool {
|
|
// 获取base64加密后的 oauth2 APP模式客户端信息
|
|
static String getLoginAuthorization() {
|
|
return "Basic ${EncryptUtil.encodeBase64(Constants.oauth2AppClient)}";
|
|
}
|
|
|
|
/// 按 Web 端约定使用 AES-CFB、Key 同值 IV、NoPadding 加密登录密码。
|
|
static String encryptLoginPassword(String password) {
|
|
final keyBytes = Uint8List.fromList(utf8.encode(Constants.pwdEncKey));
|
|
final passwordBytes = Uint8List.fromList(utf8.encode(password));
|
|
final cipher = AESEngine()..init(true, KeyParameter(keyBytes));
|
|
final encrypted = Uint8List(passwordBytes.length);
|
|
var feedback = Uint8List.fromList(keyBytes);
|
|
|
|
for (var offset = 0; offset < passwordBytes.length; offset += 16) {
|
|
final streamBlock = Uint8List(16);
|
|
cipher.processBlock(feedback, 0, streamBlock, 0);
|
|
final blockLength = (passwordBytes.length - offset).clamp(0, 16).toInt();
|
|
for (var index = 0; index < blockLength; index++) {
|
|
encrypted[offset + index] =
|
|
passwordBytes[offset + index] ^ streamBlock[index];
|
|
}
|
|
if (blockLength == 16) {
|
|
feedback = Uint8List.fromList(
|
|
encrypted.sublist(offset, offset + blockLength),
|
|
);
|
|
}
|
|
}
|
|
return base64Encode(encrypted);
|
|
}
|
|
}
|