/// <summary> /// Generate a HA1 hash /// </summary> /// <param name="realm">Realm that the user want to authenticate in</param> /// <param name="userName">User Name</param> /// <param name="password">Password</param> /// /// <para> /// A HA1 hash is simply a Md5 encoded string: "UserName:Realm:Password". The quotes should /// not be included. Realm is the currently requested Host (as in <c>Request.Headers["host"]</c>). /// </para> /// <returns></returns> private string Encode(string realm, string userName, string password) { string toHash = String.Concat(userName, ":", realm, ":", password); KeyedHashAlgorithm crypto = new KeyedHashAlgorithm(KeyedHashAlgorithmType.HMACMD5); byte[] value = crypto.ComputeHash(Encoding.UTF8.GetBytes(toHash)); return(ByteUtility.GetString(value)); }
public TeamCharacterStatus(byte[] data) { var byteOffset = 0; var id = ByteUtility.GetInt(data, ref byteOffset); var name = ByteUtility.GetString(data, ref byteOffset); var rank = ByteUtility.GetShort(data, ref byteOffset); var lastOnline = ByteUtility.GetInt(data, ref byteOffset); this.Character = new TeamCharacter { Id = id, Name = name, Rank = rank, LastOnline = lastOnline }; }
/// <summary> /// Authorize a request. /// </summary> /// <param name="request">Request being authenticated</param> /// <returns>Authenticated user if successful; otherwise null.</returns> public string Authenticate(IHttpContext context) { string authHeader = context.Request.Headers["Authorization"]; if (authHeader == null) { return(null); } authHeader = authHeader.Replace("Basic ", string.Empty); string decoded = ByteUtility.GetString(Convert.FromBase64String(authHeader)); int pos = decoded.IndexOf(':'); if (pos == -1) { Logger.WriteDebug(this, "Invalid basic authentication header, failed to find colon. Header: " + authHeader); return(null); } string password = decoded.Substring(pos + 1, decoded.Length - pos - 1); string userName = decoded.Substring(0, pos); var user = _userService.Lookup(userName, context.Request.Uri); if (user == null) { return(null); } if (user.Password == null) { string ha1 = Encode(context.Request.Uri.Host, userName, password); if (ha1 != user.HA1) { Logger.WriteInfo(this, "Incorrect user name or password. User Name: " + user.Username); return(null); } } else { if (userName != user.Username || password != user.Password) { Logger.WriteInfo(this, "Incorrect user name or password. User Name: " + user.Username); return(null); } } return(user.Username); }
public CharacterList(byte[] data) { // TODO[CJ] Clean up var characters = new List <Character>(); var byteOffset = 0; var characterCount = BitConverter.ToInt16(data, byteOffset); byteOffset += sizeof(short); for (var i = 0; i < characterCount; ++i) { if (byteOffset < data.Length) { var id = ByteUtility.GetInt(data, ref byteOffset); var name = ByteUtility.GetString(data, ref byteOffset); var unknown1 = ByteUtility.GetInt(data, ref byteOffset); var unknown2 = ByteUtility.GetInt(data, ref byteOffset); var unknown3 = ByteUtility.GetInt(data, ref byteOffset); var className = ByteUtility.GetString(data, ref byteOffset); var unknown4 = ByteUtility.GetInt(data, ref byteOffset); if (byteOffset < data.Length) { var hasSkins = ByteUtility.GetBoolean(data, ref byteOffset); if (hasSkins) { var skinCount = ByteUtility.GetInt(data, ref byteOffset); for (var j = 0; j < skinCount; ++j) { var unknown5 = ByteUtility.GetByte(data, ref byteOffset); var skinName = ByteUtility.GetString(data, ref byteOffset); var skinName2 = ByteUtility.GetString(data, ref byteOffset); var unknown6 = ByteUtility.GetLong(data, ref byteOffset); var unknown7 = ByteUtility.GetByte(data, ref byteOffset); for (var k = 0; k < 6; ++k) { var skinFile = ByteUtility.GetString(data, ref byteOffset); } } } } characters.Add(new Character { Id = id, Name = name }); } } var test = ByteUtility.ByteArrayToHexString(data); this.Characters = characters.ToArray(); }
public TextMessage(byte[] data) { var offset = 0; var type = ByteUtility.GetByte(data, ref offset); var message = ByteUtility.GetString(data, ref offset); var username = string.Empty; if (offset < data.Length) { ByteUtility.GetByte(data, ref offset); username = ByteUtility.GetString(data, ref offset); } this.Message = new ChatMessage(type, message, username); }
public void Archive_GetArchive_Files() { var repositoryFiles = new[] { new RepositoryFile("Private/PrivateFile1.java", "ModifiedByStudent"), new RepositoryFile("Immutable/ImmutableFile1.java", "ModifiedByStudent"), new RepositoryFile("Public/PublicFile1.java", "ModifiedByStudent"), new RepositoryFile("Public/PublicFile2.txt", "ModifiedByStudent"), new RepositoryFile("Public/PublicFile2.bin", new byte[] { 0xFF, 0xFE }) }; int iFile = 0; using (var submissionContents = GetArchive(repositoryFiles)) { submissionContents.Should().NotBeNull().And.Subject.Should().BeAssignableTo <IArchive>(); foreach (var archiveFile in submissionContents.Files) { archiveFile.FullPath.Should().Be(repositoryFiles[iFile].Path); if (archiveFile.FullPath.EndsWith(".bin")) { archiveFile.Ascii.Should().BeFalse(); archiveFile.GetEncodedData().Should().Be(Convert.ToBase64String(archiveFile.GetRawData())); } else { ByteUtility.GetString(archiveFile.GetRawData()).Should().Be(repositoryFiles[iFile].Contents); archiveFile.Ascii.Should().BeTrue(); archiveFile.GetEncodedData().Should().Be(repositoryFiles[iFile].Contents); } iFile++; } } }
public LoginFail(byte[] data) { var offset = 0; this.FailureReason = ByteUtility.GetString(data, ref offset); }
/// <summary> /// Initializes a new instance of the <see cref="DhcpMessageEventArgs"/> class. /// </summary> /// <param name="channel">Socket channel request is recived on.</param> /// <param name="data">Raw data received from socket.</param> public DhcpMessageEventArgs(SocketChannel channel, SocketBuffer data) { if (channel == null) { throw new ArgumentNullException("channel"); } this.Channel = channel; if (data == null) { throw new ArgumentNullException("data"); } this.ChannelBuffer = data; try { // Parse the dhcp message this.RequestMessage = new DhcpMessage(data.Buffer); // get message options MessageOptions options = new MessageOptions(); options.MessageType = this.RequestMessage.GetOptionData(DhcpOption.DhcpMessageType); // get message type option options.ClientId = this.RequestMessage.GetOptionData(DhcpOption.ClientId); if (options.ClientId == null) { // if the client id is not provided use the client hardware address from the message options.ClientId = this.RequestMessage.ClientHardwareAddress.ToArray(); } // get host name option options.HostName = this.RequestMessage.GetOptionData(DhcpOption.Hostname); // get address request option options.AddressRequest = this.RequestMessage.GetOptionData(DhcpOption.AddressRequest); //get server identifier option options.ServerIdentifier = this.RequestMessage.GetOptionData(DhcpOption.ServerIdentifier); // get paramerter list opiton options.ParameterList = this.RequestMessage.GetOptionData(DhcpOption.ParameterList); // set the response options object this.RequestOptions = options; // set the response binding object ResponseBinding = new BindingLease( ByteUtility.GetString(this.RequestOptions.ClientId), this.RequestMessage.ClientHardwareAddress, this.RequestMessage.ClientAddress, this.RequestOptions.HostName, DateTime.MinValue, this.RequestMessage.SessionId, LeaseState.Unassigned); // log that the packet was successfully parsed Logger.WriteDebug(this, "PACKET with message id " + this.RequestMessage.SessionId.ToHexString("0x") + " successfully parsed from client endpoint " + this.Channel.RemoteEndpoint.ToString()); Logger.WriteDebug(this.RequestMessage.ToString()); } catch (Exception ex) { Logger.WriteError(this, "Error parsing message:" + ex.Message.ToString(), ex); return; } }