/// <summary> /// 从流中读取文件头 /// </summary> /// <param name="stream"></param> /// <returns></returns> public static AbHeader From(Stream stream) { var header = new AbHeader(); if (stream == null) { return(null); } // 魔术字 ==> 1 header.Magic = ReadLine(stream); if (!string.Equals(header.Magic, HEADER_MAGIC)) { return(null); } // 备份文件版本 ==> 2 header.Version = ReadInt(stream); if (header.Version == -1) { return(null); } // 是否经过压缩 ==> 3 header.Compressed = ReadInt(stream) == 1; // 加密算法 ==> 4 header.Algorithm = ReadLine(stream); if (string.Equals(header.Algorithm, ENCRYPTION_ALGORITHM)) { header.IsEncrypted = true; } // 如果未加密,文件头解析结束 if (!header.IsEncrypted) { header.Valid = true; return(header); } // 用户盐 ==> 5 header.UserSalt = ReadBytes(stream); if (header.UserSalt.Length != PBKDF2_SALT_SIZE / 8) { header.Valid = false; return(null); } // 用户盐校验和 ==> 6 header.SaltCheckSum = ReadBytes(stream); // 循环数 ==> 7 header.Rounds = ReadInt(stream); // 用户向量 ==> 8 header.UserIv = ReadBytes(stream); // 主密钥 ==> 9 header.MasterKey = ReadBytes(stream); // Header读取成功 header.Valid = true; return(header); }
/// <summary> /// 读取备份文件,获取实例 /// </summary> /// <param name="path"></param> /// <param name="handler"></param> /// <returns></returns> public static AbFile With(string path, Func <bool, PasswdResult> handler) { var file = new AbFile { PasswdHandler = handler }; try { // 读取备份文件流 file.Stream = File.OpenRead(path); } catch (Exception exception) { // 记录日志 Logger.Error(exception); return(null); } file.header = AbHeader.From(file.Stream); return(file); }