1. Android: 组合多个硬件标识 + 序列号 + Build信息

2. iOS: 组合设备型号 + 系统版本 + identifierForVendor,持久化存储到钥匙串
  3. macOS: 使用硬件UUID + 序列号
  4. Windows: 使用主板UUID + CPU信息
  5. Linux: 使用machine-id + 硬件信息

(cherry picked from commit 1be3037f715548a1efa4cc5d7d204b989878557a)
This commit is contained in:
Rust 2025-10-31 11:16:35 +08:00 committed by speakeloudest
parent c1c5f1a2e0
commit 5c8f0ca1fc

View File

@ -1,6 +1,7 @@
import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_udid/flutter_udid.dart';
import '../utils/kr_secure_storage.dart';
import '../utils/kr_log_util.dart';
import 'package:crypto/crypto.dart';
@ -51,37 +52,28 @@ class KRDeviceInfoService {
}
///
/// 使
Future<String> _getDeviceId() async {
try {
String? identifier;
String identifier;
if (Platform.isAndroid) {
final androidInfo = await _deviceInfo.androidInfo;
// Android使用androidId作为唯一标识
identifier = androidInfo.id;
identifier = await _getAndroidDeviceId();
} else if (Platform.isIOS) {
final iosInfo = await _deviceInfo.iosInfo;
// iOS使用identifierForVendor作为唯一标识
identifier = iosInfo.identifierForVendor;
identifier = await _getIOSDeviceId();
} else if (Platform.isMacOS) {
final macInfo = await _deviceInfo.macOsInfo;
// macOS使用systemGUID
identifier = macInfo.systemGUID;
identifier = await _getMacOSDeviceId();
} else if (Platform.isWindows) {
final windowsInfo = await _deviceInfo.windowsInfo;
// Windows使用计算机名作为唯一标识
identifier = windowsInfo.computerName;
identifier = await _getWindowsDeviceId();
} else if (Platform.isLinux) {
final linuxInfo = await _deviceInfo.linuxInfo;
// Linux使用machineId
identifier = linuxInfo.machineId;
identifier = await _getLinuxDeviceId();
} else {
// Web或其他平台使UUID
// Web或其他平台,使UUID
identifier = await _getOrCreateStoredDeviceId();
}
// 使ID
if (identifier == null || identifier.isEmpty) {
// ,使ID
if (identifier.isEmpty) {
identifier = await _getOrCreateStoredDeviceId();
}
@ -89,11 +81,201 @@ class KRDeviceInfoService {
} catch (e) {
print('❌ 获取设备ID失败: $e');
KRLogUtil.kr_e('❌ 获取设备ID失败 - $e', tag: 'KRDeviceInfoService');
// ID
// ,ID
return await _getOrCreateStoredDeviceId();
}
}
/// Android设备ID -
/// : AndroidID + + +
Future<String> _getAndroidDeviceId() async {
try {
final androidInfo = await _deviceInfo.androidInfo;
// 使 flutter_udid (Android标识获取方式)
String udid = await FlutterUdid.consistentUdid;
//
final factors = [
udid,
androidInfo.id, // Android ID
androidInfo.board, //
androidInfo.bootloader, // Bootloader
androidInfo.brand, //
androidInfo.device, //
androidInfo.fingerprint, //
androidInfo.hardware, //
androidInfo.manufacturer, //
androidInfo.model, //
androidInfo.product, //
];
//
final combined = factors
.where((f) => f != null && f.isNotEmpty)
.join('|');
// SHA256哈希
final bytes = utf8.encode(combined);
final hash = sha256.convert(bytes);
print('📱 Android多因子ID生成 - 因子数: ${factors.where((f) => f != null && f.isNotEmpty).length}');
KRLogUtil.kr_i('📱 Android多因子ID - $hash', tag: 'KRDeviceInfoService');
return hash.toString();
} catch (e) {
print('❌ Android设备ID获取失败: $e');
KRLogUtil.kr_e('❌ Android设备ID获取失败 - $e', tag: 'KRDeviceInfoService');
return '';
}
}
/// iOS设备ID - 使identifierForVendor,
/// iOS限制较严,IMEI等敏感信息
Future<String> _getIOSDeviceId() async {
try {
final iosInfo = await _deviceInfo.iosInfo;
// 使 flutter_udid
String udid = await FlutterUdid.consistentUdid;
//
final factors = [
udid,
iosInfo.identifierForVendor ?? '', // Vendor标识
iosInfo.model, //
iosInfo.systemName, //
iosInfo.systemVersion, //
iosInfo.name, // ("iPhone 14 Pro")
iosInfo.utsname.machine, //
];
final combined = factors
.where((f) => f.isNotEmpty)
.join('|');
final bytes = utf8.encode(combined);
final hash = sha256.convert(bytes);
print('📱 iOS多因子ID生成 - 因子数: ${factors.where((f) => f.isNotEmpty).length}');
KRLogUtil.kr_i('📱 iOS多因子ID - $hash', tag: 'KRDeviceInfoService');
return hash.toString();
} catch (e) {
print('❌ iOS设备ID获取失败: $e');
KRLogUtil.kr_e('❌ iOS设备ID获取失败 - $e', tag: 'KRDeviceInfoService');
return '';
}
}
/// macOS设备ID - 使UUID
Future<String> _getMacOSDeviceId() async {
try {
final macInfo = await _deviceInfo.macOsInfo;
// 使 flutter_udid
String udid = await FlutterUdid.consistentUdid;
//
final factors = [
udid,
macInfo.systemGUID ?? '', // GUID ()
macInfo.model, //
macInfo.hostName, //
macInfo.arch, //
macInfo.kernelVersion, //
];
final combined = factors
.where((f) => f.isNotEmpty)
.join('|');
final bytes = utf8.encode(combined);
final hash = sha256.convert(bytes);
print('📱 macOS多因子ID生成 - 因子数: ${factors.where((f) => f.isNotEmpty).length}');
KRLogUtil.kr_i('📱 macOS多因子ID - $hash', tag: 'KRDeviceInfoService');
return hash.toString();
} catch (e) {
print('❌ macOS设备ID获取失败: $e');
KRLogUtil.kr_e('❌ macOS设备ID获取失败 - $e', tag: 'KRDeviceInfoService');
return '';
}
}
/// Windows设备ID - 使GUID
Future<String> _getWindowsDeviceId() async {
try {
final windowsInfo = await _deviceInfo.windowsInfo;
// 使 flutter_udid
String udid = await FlutterUdid.consistentUdid;
//
final factors = [
udid,
windowsInfo.deviceId, // ID
windowsInfo.computerName, //
windowsInfo.productName, //
windowsInfo.numberOfCores.toString(), // CPU核心数
windowsInfo.systemMemoryInMegabytes.toString(), //
];
final combined = factors
.where((f) => f.isNotEmpty)
.join('|');
final bytes = utf8.encode(combined);
final hash = sha256.convert(bytes);
print('📱 Windows多因子ID生成 - 因子数: ${factors.where((f) => f.isNotEmpty).length}');
KRLogUtil.kr_i('📱 Windows多因子ID - $hash', tag: 'KRDeviceInfoService');
return hash.toString();
} catch (e) {
print('❌ Windows设备ID获取失败: $e');
KRLogUtil.kr_e('❌ Windows设备ID获取失败 - $e', tag: 'KRDeviceInfoService');
return '';
}
}
/// Linux设备ID - 使machine-id
Future<String> _getLinuxDeviceId() async {
try {
final linuxInfo = await _deviceInfo.linuxInfo;
// 使 flutter_udid
String udid = await FlutterUdid.consistentUdid;
//
final factors = [
udid,
linuxInfo.machineId ?? '', // Machine ID ()
linuxInfo.id, // ID
linuxInfo.name, //
linuxInfo.version ?? '', //
linuxInfo.variant ?? '', //
];
final combined = factors
.where((f) => f.isNotEmpty)
.join('|');
final bytes = utf8.encode(combined);
final hash = sha256.convert(bytes);
print('📱 Linux多因子ID生成 - 因子数: ${factors.where((f) => f.isNotEmpty).length}');
KRLogUtil.kr_i('📱 Linux多因子ID - $hash', tag: 'KRDeviceInfoService');
return hash.toString();
} catch (e) {
print('❌ Linux设备ID获取失败: $e');
KRLogUtil.kr_e('❌ Linux设备ID获取失败 - $e', tag: 'KRDeviceInfoService');
return '';
}
}
/// ID
Future<String> _getOrCreateStoredDeviceId() async {
try {