The .NET Panel class has a method named PointToClient that accepts a Point as input. The Point should be in screen coordinates (in your case; the mouse cursor position relative to the screen itself), the result is a Point that is relative to the Panel.
- Viewing Profile: Reputation: btower
Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics
Community Stats
- Group Members
- Active Posts 80
- Profile Views 992
- Member Title GDNet+
- Age Age Unknown
- Birthday Birthday Unknown
-
Gender
Male
-
Location
Enschede, The Netherlands
-
Interests
Software Engineering
516
Excellent
User Tools
#5034955 Getting X/Y of mouse ONLY inside rendered area
Posted by btower
on 21 February 2013 - 06:19 AM
#5007197 Big Array to String Speed
Posted by btower
on 04 December 2012 - 03:39 PM
How about the following:
[source lang="csharp"] //this just generates a "big string array" to play with List<string> bigList = new List<string>(); for (int i = 0; i < 25000; ++i) bigList.Add("someMediumLengthString"); string[] bigStringArray = bigList.ToArray(); //measure time DateTime start = DateTime.Now; //now we do the part that matters string ourHugeCombinedString = string.Join<string>('\n'.ToString(), bigStringArray); //print out the time it took to make the giant string TimeSpan timeToMakeCombinedString = DateTime.Now.Subtract(start); System.Diagnostics.Debug.WriteLine(timeToMakeCombinedString.TotalMilliseconds.ToString()); //prints out "2,0001" on my system, so 2 ms![/source]
[source lang="csharp"] //this just generates a "big string array" to play with List<string> bigList = new List<string>(); for (int i = 0; i < 25000; ++i) bigList.Add("someMediumLengthString"); string[] bigStringArray = bigList.ToArray(); //measure time DateTime start = DateTime.Now; //now we do the part that matters string ourHugeCombinedString = string.Join<string>('\n'.ToString(), bigStringArray); //print out the time it took to make the giant string TimeSpan timeToMakeCombinedString = DateTime.Now.Subtract(start); System.Diagnostics.Debug.WriteLine(timeToMakeCombinedString.TotalMilliseconds.ToString()); //prints out "2,0001" on my system, so 2 ms![/source]
#4945241 A few quick math questions
Posted by btower
on 01 June 2012 - 02:13 AM
I refer you to this blog post I found that explains all the concepts above in plain English with game-related examples. Sounds like it's exactly what you're looking for: http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/
Hope it helps!
Hope it helps!
#4942210 Signing a message
Posted by btower
on 22 May 2012 - 08:24 AM
I've written this once for VB.NET. I hope you can read it and easily convert it to C#. It's not very difficult to do in .NET. I just use these two small helper classes for it.
As you mentioned, you found a lot of pointers on how to generate the certificate itself - so I won't explain that here.
As you mentioned, you found a lot of pointers on how to generate the certificate itself - so I won't explain that here.
Imports System.IO
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Namespace Security.Cryptography
''' <summary>
''' Provides methods to sign and verify SHA1/RSA 1024 bits signatures.
''' </summary>
''' <remarks></remarks>
<DebuggerNonUserCode()> _
Public NotInheritable Class SHA1RSASigner
''' <summary>
''' Signs a string using SHA-1 hashing, then 1024 bits RSA signing (private key is necessairy) and returns the result base64 encoded.
''' </summary>
''' <param name="cert">A certificate containing a private key. This will be used to make an authentic signature.</param>
''' <param name="stringToSign">The string to make a signature for.</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function CreateSignatureBase64(ByVal cert As Certificate, ByVal stringToSign As String) As String
If (cert Is Nothing) Then Return String.Empty
If (Not cert.HasPrivateKey) Then Return String.Empty
If (String.IsNullOrEmpty(stringToSign)) Then Return String.Empty
Dim bytes() As Byte = Encoding.ASCII.GetBytes(stringToSign)
Dim sha As New SHA1Managed
sha.Initialize()
Dim shaDigest() As Byte = sha.ComputeHash(bytes)
Dim rsa As RSACryptoServiceProvider = cert.GetRSACryptoServiceProvider()
Dim rsaDigest() As Byte = rsa.SignHash(shaDigest, CryptoConfig.MapNameToOID("SHA1"))
Return Convert.ToBase64String(rsaDigest)
End Function
''' <summary>
''' Verifies a (plain-text) String based on a digital signature that is SHA-1 hashed, then RSA 1024 bits signed and finally base64 encoded.
''' </summary>
''' <param name="publicKey">The public key of the one who signed the message (the iDEAL acquirer's certificate).</param>
''' <param name="originalString">Plain-text string containing the signed text.</param>
''' <param name="base64TokenToVerify">The base64 encoded digital signature created by the sender (iDEAL acquirer).</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function VerifySignatureBase64(ByVal publicKey As Certificate, ByVal originalString As String, ByVal base64TokenToVerify As String) As Boolean
If (publicKey Is Nothing) Then Return False
If (String.IsNullOrEmpty(originalString)) Then Return False
If (String.IsNullOrEmpty(base64TokenToVerify)) Then Return False
Dim bytes() As Byte = Encoding.ASCII.GetBytes(originalString)
Dim sha As New SHA1Managed
sha.Initialize()
Dim shaDigestOriginal() As Byte = sha.ComputeHash(bytes)
bytes = Convert.FromBase64String(base64TokenToVerify)
Dim rsa As RSACryptoServiceProvider = publicKey.GetRSACryptoServiceProvider()
Return rsa.VerifyHash(shaDigestOriginal, CryptoConfig.MapNameToOID("SHA1"), bytes)
End Function
End Class
End Namespace
Imports System.IO
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Namespace Security.Cryptography
''' <summary>
''' Represents a X509 certificate (PKCS #12).
''' </summary>
''' <remarks></remarks>
<DebuggerNonUserCode()> _
Public NotInheritable Class Certificate
Private _certificate As X509Certificate2
#Region " Properties "
''' <summary>
''' Gets the fingerprint of the certificate.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property FingerPrint() As String
Get
If (_certificate Is Nothing) Then Return String.Empty
Return _certificate.Thumbprint
End Get
End Property
''' <summary>
''' Gets wether this certificate contains a private key.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property HasPrivateKey() As Boolean
Get
If (_certificate Is Nothing) Then Return False
Return _certificate.HasPrivateKey
End Get
End Property
''' <summary>
''' Gets the private key of this certificate, if available.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property PrivateKey() As String
Get
If ((_certificate Is Nothing) OrElse (_certificate.PrivateKey Is Nothing)) Then Throw New CertificateException("This certificate does not contain a private key.")
Try
Return _certificate.PrivateKey.ToXmlString(True)
Catch ex As Exception
Throw New CertificateException(String.Concat(ex.Message, " See InnerException for more details."), ex)
End Try
End Get
End Property
''' <summary>
''' Gets the public key of this certificate, if available.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property PublicKey() As String
Get
If ((_certificate Is Nothing) OrElse (_certificate.PublicKey Is Nothing) OrElse (_certificate.PublicKey.Key Is Nothing)) Then Throw New CertificateException("This certificate does not contain a public key.")
Try
Return _certificate.PublicKey.Key.ToXmlString(False)
Catch ex As Exception
Throw New CertificateException(String.Concat(ex.Message, " See InnerException for more details."), ex)
End Try
End Get
End Property
#End Region
''' <summary>
''' Loads an existing certificate based on a PKCS #12 file.
''' </summary>
''' <param name="fileName">Virtual path to a PKCS #12 certificate file. Supports CER (publickey) and P12 (privatekey) files.</param>
''' <param name="password">Optional password if the file requires one.</param>
Public Sub New(ByVal fileName As String, Optional ByVal password As String = Nothing)
Try
Dim certificateBytes() As Byte
Dim context As HttpContext = HttpContext.Current
If (context Is Nothing) Then
certificateBytes = File.ReadAllBytes(fileName.Replace("/"c, "\"c).Replace("~", Environment.CurrentDirectory))
Else
certificateBytes = File.ReadAllBytes(context.Request.MapPath(fileName))
End If
If ((certificateBytes IsNot Nothing) AndAlso (certificateBytes.Length > 0)) Then
_certificate = New X509Certificate2(certificateBytes, password, X509KeyStorageFlags.Exportable Or X509KeyStorageFlags.MachineKeySet Or X509KeyStorageFlags.PersistKeySet)
End If
Catch ex As Exception
Throw New CertificateException(String.Concat("Failed to load certificate file: ", ex.Message, " See InnerException for more details."), ex)
End Try
End Sub
''' <summary>
''' Creates an RSA Crypto Service provider that is initialized with this certificate's key. The private key is used, if possible. Keysize is set to 1024 bits.
''' </summary>
Public Function GetRSACryptoServiceProvider() As RSACryptoServiceProvider
RSACryptoServiceProvider.UseMachineKeyStore = True
Dim result As New RSACryptoServiceProvider
result.KeySize = 1024
If (Me.HasPrivateKey) Then
result.FromXmlString(Me.PrivateKey)
Else
result.FromXmlString(Me.PublicKey)
End If
Return result
End Function
End Class
End Namespace
#4781460 To those of you who can't kill your program after pressing "X" bu...
Posted by btower
on 03 March 2011 - 11:44 AM
To put Endurion's words into code, see below.
Your main loop could be simpler:
The WndProc:
Your main loop could be simpler:
MSG msg = {0};
while (msg.message != WM_QUIT)
{
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// game stuff here
}The WndProc:
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_CLOSE:
DestroyWindow(hWnd);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
#4773159 password encryption
Posted by btower
on 11 February 2011 - 07:44 PM
You don't really need to encrypt the passwords. You can alternatively hash them instead. This way you can compare the user input (also hashed) to your database entry. This is already plenty safe. I would suggest you use the SHA1 hash algorithm for this.
- Home
- » Viewing Profile: Reputation: btower

Find content