public static AuthKey Deserialize(Bytes key) => key.ToArrayUnsafe().Apply(Helpers.Sha1).Apply(BtHelpers.Deserialize(br => { var auxHash = br.ReadUInt64(); br.ReadBytes(4); var keyId = br.ReadUInt64(); return(new AuthKey(key, keyId, auxHash)); }));
public static Message ReadMsg(BinaryReader br) { var id = br.ReadInt64(); var seqNo = br.ReadInt32(); var bodyLength = br.ReadInt32(); var body = br.ReadBytes(bodyLength); return(new Message(id, seqNo, body.Apply(BtHelpers.Deserialize(identity)))); }
static async Task <Option <Session> > Read(string fileName) { if (!File.Exists(fileName)) { return(None); } var bts = await FileHelpers.ReadFileBytes(fileName); return(bts.Apply(BtHelpers.Deserialize(Session.Deserialize))); }
public async Task <BinaryReader> Receive() { var plainText = await ReceivePlainText(); return(plainText.Apply(BtHelpers.Deserialize(br => { var remoteSalt = br.ReadUInt64(); var remoteSessionId = br.ReadUInt64(); return br; }))); }
async Task <byte[]> ReceivePlainText() { var body = await _transport.Receive(); return(body.Apply(BtHelpers.Deserialize(br => { var authKeyId = br.ReadUInt64(); // TODO: check auth key id var msgKey = br.ReadBytes(16); // TODO: check msg_key correctness var keyData = CalcAesKey(_session.Get().AuthKey.Key.ToArrayUnsafe(), msgKey, false); var bs = br.BaseStream; var cipherTextLen = (int)(bs.Length - bs.Position); var cipherText = br.ReadBytes(cipherTextLen); var plainText = Aes.DecryptAES(keyData, cipherText); return plainText; }))); }