[pgsqlclient-checkins] pgsqlclient_10/Mono.Security.Protocol.Tls/Mono.Security.Protocol.Tls/Mono.Sec
Status: Inactive
Brought to you by:
carlosga_fb
From: <car...@us...> - 2004-02-10 03:07:36
|
Update of /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security.Protocol.Tls/Mono.Security.Protocol.Tls/Mono.Security/Mono.Security.Cryptography In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12935 Modified Files: HMAC.cs MD5SHA1.cs Log Message: Updated license headers Index: HMAC.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security.Protocol.Tls/Mono.Security.Protocol.Tls/Mono.Security/Mono.Security.Cryptography/HMAC.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** HMAC.cs 14 Dec 2003 14:59:55 -0000 1.4 --- HMAC.cs 9 Feb 2004 14:18:39 -0000 1.5 *************** *** 1,191 **** ! /* Transport Security Layer (TLS) ! * Copyright (c) 2003 Carlos Guzmán Álvarez ! * ! * Permission is hereby granted, free of charge, to any person ! * obtaining a copy of this software and associated documentation ! * files (the "Software"), to deal in the Software without restriction, ! * including without limitation the rights to use, copy, modify, merge, ! * publish, distribute, sublicense, and/or sell copies of the Software, ! * and to permit persons to whom the Software is furnished to do so, ! * subject to the following conditions: ! * ! * The above copyright notice and this permission notice shall be included ! * in all copies or substantial portions of the Software. ! * ! * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ! * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES ! * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ! * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT ! * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, ! * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ! * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER ! * DEALINGS IN THE SOFTWARE. ! */ ! ! using System; ! using System.Security.Cryptography; ! ! namespace Mono.Security.Cryptography ! { ! /* ! * References: ! * RFC 2104 (http://www.ietf.org/rfc/rfc2104.txt) ! * RFC 2202 (http://www.ietf.org/rfc/rfc2202.txt) ! * MSDN: ! * ! * Extending the KeyedHashAlgorithm Class (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconextendingkeyedhashalgorithmclass.asp) ! */ ! internal class HMAC : System.Security.Cryptography.KeyedHashAlgorithm ! { ! #region Fields ! ! private HashAlgorithm hash; ! private bool hashing; ! ! private byte[] innerPad; ! private byte[] outerPad; ! ! #endregion ! ! #region Properties ! ! public override byte[] Key ! { ! get { return (byte[])KeyValue.Clone(); } ! set ! { ! if (hashing) ! { ! throw new Exception("Cannot change key during hash operation."); ! } ! ! /* if key is longer than 64 bytes reset it to rgbKey = Hash(rgbKey) */ ! if (value.Length > 64) ! { ! KeyValue = hash.ComputeHash(value); ! } ! else ! { ! KeyValue = (byte[])value.Clone(); ! } ! ! initializePad(); ! } ! } ! ! #endregion ! ! #region Constructors ! ! public HMAC() ! { ! // Create the hash ! hash = MD5.Create(); ! // Set HashSizeValue ! HashSizeValue = hash.HashSize; ! ! // Generate a radom key ! byte[] rgbKey = new byte[64]; ! RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); ! rng.GetNonZeroBytes(rgbKey); ! ! KeyValue = (byte[])rgbKey.Clone(); ! ! this.Initialize(); ! } ! ! public HMAC(string hashName, byte[] rgbKey) ! { ! // Create the hash ! if (hashName == null || hashName.Length == 0) ! { ! hashName = "MD5"; ! } ! hash = HashAlgorithm.Create(hashName); ! // Set HashSizeValue ! HashSizeValue = hash.HashSize; ! ! /* if key is longer than 64 bytes reset it to rgbKey = Hash(rgbKey) */ ! if (rgbKey.Length > 64) ! { ! KeyValue = hash.ComputeHash(rgbKey); ! } ! else ! { ! KeyValue = (byte[])rgbKey.Clone(); ! } ! ! this.Initialize(); ! } ! ! #endregion ! ! #region Methods ! ! public override void Initialize() ! { ! hash.Initialize(); ! initializePad(); ! hashing = false; ! } ! ! protected override byte[] HashFinal() ! { ! if (!hashing) ! { ! hash.TransformBlock(innerPad, 0, innerPad.Length, innerPad, 0); ! hashing = true; ! } ! // Finalize the original hash ! hash.TransformFinalBlock(new byte[0], 0, 0); ! ! byte[] firstResult = hash.Hash; ! ! hash.Initialize(); ! hash.TransformBlock(outerPad, 0, outerPad.Length, outerPad, 0); ! hash.TransformFinalBlock(firstResult, 0, firstResult.Length); ! ! Initialize(); ! ! return hash.Hash; ! } ! ! protected override void HashCore( ! byte[] array, ! int ibStart, ! int cbSize) ! { ! if (!hashing) ! { ! hash.TransformBlock(innerPad, 0, innerPad.Length, innerPad, 0); ! hashing = true; ! } ! hash.TransformBlock(array, ibStart, cbSize, array, ibStart); ! } ! ! #endregion ! ! #region Private Methods ! ! private void initializePad() ! { ! // Fill pad arrays ! innerPad = new byte[64]; ! outerPad = new byte[64]; ! ! /* Pad the key for inner and outer digest */ ! for (int i = 0 ; i < KeyValue.Length; ++i) ! { ! innerPad[i] = (byte)(KeyValue[i] ^ 0x36); ! outerPad[i] = (byte)(KeyValue[i] ^ 0x5C); ! } ! for (int i = KeyValue.Length; i < 64; ++i) ! { ! innerPad[i] = 0x36; ! outerPad[i] = 0x5C; ! } ! } ! ! #endregion ! } ! } --- 1,191 ---- ! /* Transport Security Layer (TLS) ! * Copyright (c) 2003-2004 Carlos Guzman Alvarez ! * ! * Permission is hereby granted, free of charge, to any person ! * obtaining a copy of this software and associated documentation ! * files (the "Software"), to deal in the Software without restriction, ! * including without limitation the rights to use, copy, modify, merge, ! * publish, distribute, sublicense, and/or sell copies of the Software, ! * and to permit persons to whom the Software is furnished to do so, ! * subject to the following conditions: ! * ! * The above copyright notice and this permission notice shall be included ! * in all copies or substantial portions of the Software. ! * ! * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ! * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES ! * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ! * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT ! * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, ! * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ! * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER ! * DEALINGS IN THE SOFTWARE. ! */ ! ! using System; ! using System.Security.Cryptography; ! ! namespace Mono.Security.Cryptography ! { ! /* ! * References: ! * RFC 2104 (http://www.ietf.org/rfc/rfc2104.txt) ! * RFC 2202 (http://www.ietf.org/rfc/rfc2202.txt) ! * MSDN: ! * ! * Extending the KeyedHashAlgorithm Class (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconextendingkeyedhashalgorithmclass.asp) ! */ ! internal class HMAC : System.Security.Cryptography.KeyedHashAlgorithm ! { ! #region Fields ! ! private HashAlgorithm hash; ! private bool hashing; ! ! private byte[] innerPad; ! private byte[] outerPad; ! ! #endregion ! ! #region Properties ! ! public override byte[] Key ! { ! get { return (byte[])KeyValue.Clone(); } ! set ! { ! if (hashing) ! { ! throw new Exception("Cannot change key during hash operation."); ! } ! ! /* if key is longer than 64 bytes reset it to rgbKey = Hash(rgbKey) */ ! if (value.Length > 64) ! { ! KeyValue = hash.ComputeHash(value); ! } ! else ! { ! KeyValue = (byte[])value.Clone(); ! } ! ! initializePad(); ! } ! } ! ! #endregion ! ! #region Constructors ! ! public HMAC() ! { ! // Create the hash ! hash = MD5.Create(); ! // Set HashSizeValue ! HashSizeValue = hash.HashSize; ! ! // Generate a radom key ! byte[] rgbKey = new byte[64]; ! RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); ! rng.GetNonZeroBytes(rgbKey); ! ! KeyValue = (byte[])rgbKey.Clone(); ! ! this.Initialize(); ! } ! ! public HMAC(string hashName, byte[] rgbKey) ! { ! // Create the hash ! if (hashName == null || hashName.Length == 0) ! { ! hashName = "MD5"; ! } ! hash = HashAlgorithm.Create(hashName); ! // Set HashSizeValue ! HashSizeValue = hash.HashSize; ! ! /* if key is longer than 64 bytes reset it to rgbKey = Hash(rgbKey) */ ! if (rgbKey.Length > 64) ! { ! KeyValue = hash.ComputeHash(rgbKey); ! } ! else ! { ! KeyValue = (byte[])rgbKey.Clone(); ! } ! ! this.Initialize(); ! } ! ! #endregion ! ! #region Methods ! ! public override void Initialize() ! { ! hash.Initialize(); ! initializePad(); ! hashing = false; ! } ! ! protected override byte[] HashFinal() ! { ! if (!hashing) ! { ! hash.TransformBlock(innerPad, 0, innerPad.Length, innerPad, 0); ! hashing = true; ! } ! // Finalize the original hash ! hash.TransformFinalBlock(new byte[0], 0, 0); ! ! byte[] firstResult = hash.Hash; ! ! hash.Initialize(); ! hash.TransformBlock(outerPad, 0, outerPad.Length, outerPad, 0); ! hash.TransformFinalBlock(firstResult, 0, firstResult.Length); ! ! Initialize(); ! ! return hash.Hash; ! } ! ! protected override void HashCore( ! byte[] array, ! int ibStart, ! int cbSize) ! { ! if (!hashing) ! { ! hash.TransformBlock(innerPad, 0, innerPad.Length, innerPad, 0); ! hashing = true; ! } ! hash.TransformBlock(array, ibStart, cbSize, array, ibStart); ! } ! ! #endregion ! ! #region Private Methods ! ! private void initializePad() ! { ! // Fill pad arrays ! innerPad = new byte[64]; ! outerPad = new byte[64]; ! ! /* Pad the key for inner and outer digest */ ! for (int i = 0 ; i < KeyValue.Length; ++i) ! { ! innerPad[i] = (byte)(KeyValue[i] ^ 0x36); ! outerPad[i] = (byte)(KeyValue[i] ^ 0x5C); ! } ! for (int i = KeyValue.Length; i < 64; ++i) ! { ! innerPad[i] = 0x36; ! outerPad[i] = 0x5C; ! } ! } ! ! #endregion ! } ! } Index: MD5SHA1.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security.Protocol.Tls/Mono.Security.Protocol.Tls/Mono.Security/Mono.Security.Cryptography/MD5SHA1.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** MD5SHA1.cs 14 Dec 2003 15:06:18 -0000 1.4 --- MD5SHA1.cs 9 Feb 2004 14:18:39 -0000 1.5 *************** *** 1,127 **** ! /* Transport Security Layer (TLS) ! * Copyright (c) 2003 Carlos Guzmán Álvarez ! * ! * Permission is hereby granted, free of charge, to any person ! * obtaining a copy of this software and associated documentation ! * files (the "Software"), to deal in the Software without restriction, ! * including without limitation the rights to use, copy, modify, merge, ! * publish, distribute, sublicense, and/or sell copies of the Software, ! * and to permit persons to whom the Software is furnished to do so, ! * subject to the following conditions: ! * ! * The above copyright notice and this permission notice shall be included ! * in all copies or substantial portions of the Software. ! * ! * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ! * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES ! * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ! * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT ! * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, ! * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ! * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER ! * DEALINGS IN THE SOFTWARE. ! */ ! ! using System; ! using System.Security.Cryptography; ! ! using Mono.Security.Protocol.Tls; ! ! namespace Mono.Security.Cryptography ! { ! internal class MD5SHA1 : HashAlgorithm ! { ! #region Fields ! ! private HashAlgorithm md5; ! private HashAlgorithm sha; ! private bool hashing; ! ! #endregion ! ! #region Constructors ! ! public MD5SHA1() : base() ! { ! this.md5 = MD5.Create(); ! this.sha = SHA1.Create(); ! ! // Set HashSizeValue ! this.HashSizeValue = this.md5.HashSize + this.sha.HashSize; ! } ! ! #endregion ! ! #region Methods ! ! public override void Initialize() ! { ! this.md5.Initialize(); ! this.sha.Initialize(); ! this.hashing = false; ! } ! ! protected override byte[] HashFinal() ! { ! if (!hashing) ! { ! this.hashing = true; ! } ! // Finalize the original hash ! this.md5.TransformFinalBlock(new byte[0], 0, 0); ! this.sha.TransformFinalBlock(new byte[0], 0, 0); ! ! byte[] hash = new byte[36]; ! ! System.Array.Copy(this.md5.Hash, 0, hash, 0, 16); ! System.Array.Copy(this.sha.Hash, 0, hash, 16, 20); ! ! return hash; ! } ! ! protected override void HashCore( ! byte[] array, ! int ibStart, ! int cbSize) ! { ! if (!hashing) ! { ! hashing = true; ! } ! this.md5.TransformBlock(array, ibStart, cbSize, array, ibStart); ! this.sha.TransformBlock(array, ibStart, cbSize, array, ibStart); ! } ! ! public byte[] CreateSignature(RSA rsa) ! { ! if (rsa == null) ! { ! throw new CryptographicUnexpectedOperationException ("missing key"); ! } ! ! RSASslSignatureFormatter f = new RSASslSignatureFormatter(rsa); ! f.SetHashAlgorithm("MD5SHA1"); ! ! return f.CreateSignature(this.Hash); ! } ! ! public bool VerifySignature(RSA rsa, byte[] rgbSignature) ! { ! if (rsa == null) ! { ! throw new CryptographicUnexpectedOperationException ("missing key"); ! } ! if (rgbSignature == null) ! { ! throw new ArgumentNullException ("rgbSignature"); ! } ! ! RSASslSignatureDeformatter d = new RSASslSignatureDeformatter(rsa); ! d.SetHashAlgorithm("MD5SHA1"); ! ! return d.VerifySignature(this.Hash, rgbSignature); ! } ! ! #endregion ! } ! } --- 1,127 ---- ! /* Transport Security Layer (TLS) ! * Copyright (c) 2003-2004 Carlos Guzman Alvarez ! * ! * Permission is hereby granted, free of charge, to any person ! * obtaining a copy of this software and associated documentation ! * files (the "Software"), to deal in the Software without restriction, ! * including without limitation the rights to use, copy, modify, merge, ! * publish, distribute, sublicense, and/or sell copies of the Software, ! * and to permit persons to whom the Software is furnished to do so, ! * subject to the following conditions: ! * ! * The above copyright notice and this permission notice shall be included ! * in all copies or substantial portions of the Software. ! * ! * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ! * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES ! * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ! * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT ! * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, ! * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ! * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER ! * DEALINGS IN THE SOFTWARE. ! */ ! ! using System; ! using System.Security.Cryptography; ! ! using Mono.Security.Protocol.Tls; ! ! namespace Mono.Security.Cryptography ! { ! internal class MD5SHA1 : HashAlgorithm ! { ! #region Fields ! ! private HashAlgorithm md5; ! private HashAlgorithm sha; ! private bool hashing; ! ! #endregion ! ! #region Constructors ! ! public MD5SHA1() : base() ! { ! this.md5 = MD5.Create(); ! this.sha = SHA1.Create(); ! ! // Set HashSizeValue ! this.HashSizeValue = this.md5.HashSize + this.sha.HashSize; ! } ! ! #endregion ! ! #region Methods ! ! public override void Initialize() ! { ! this.md5.Initialize(); ! this.sha.Initialize(); ! this.hashing = false; ! } ! ! protected override byte[] HashFinal() ! { ! if (!hashing) ! { ! this.hashing = true; ! } ! // Finalize the original hash ! this.md5.TransformFinalBlock(new byte[0], 0, 0); ! this.sha.TransformFinalBlock(new byte[0], 0, 0); ! ! byte[] hash = new byte[36]; ! ! System.Array.Copy(this.md5.Hash, 0, hash, 0, 16); ! System.Array.Copy(this.sha.Hash, 0, hash, 16, 20); ! ! return hash; ! } ! ! protected override void HashCore( ! byte[] array, ! int ibStart, ! int cbSize) ! { ! if (!hashing) ! { ! hashing = true; ! } ! this.md5.TransformBlock(array, ibStart, cbSize, array, ibStart); ! this.sha.TransformBlock(array, ibStart, cbSize, array, ibStart); ! } ! ! public byte[] CreateSignature(RSA rsa) ! { ! if (rsa == null) ! { ! throw new CryptographicUnexpectedOperationException ("missing key"); ! } ! ! RSASslSignatureFormatter f = new RSASslSignatureFormatter(rsa); ! f.SetHashAlgorithm("MD5SHA1"); ! ! return f.CreateSignature(this.Hash); ! } ! ! public bool VerifySignature(RSA rsa, byte[] rgbSignature) ! { ! if (rsa == null) ! { ! throw new CryptographicUnexpectedOperationException ("missing key"); ! } ! if (rgbSignature == null) ! { ! throw new ArgumentNullException ("rgbSignature"); ! } ! ! RSASslSignatureDeformatter d = new RSASslSignatureDeformatter(rsa); ! d.SetHashAlgorithm("MD5SHA1"); ! ! return d.VerifySignature(this.Hash, rgbSignature); ! } ! ! #endregion ! } ! } |