From ff2d8b3f5232896e18c2fbfb74e1bb9c4a07ea3d Mon Sep 17 00:00:00 2001 From: olegkap_cp Date: Sun, 20 Jan 2013 04:47:18 +0000 Subject: [PATCH] Add support for hmac-sha2-256, hmac-sha2-256-96, hmac-md5-96 and hmac-sha1-96 --- .../Renci.SshNet/ConnectionInfo.cs | 10 ++++---- .../Security/Cryptography/HMAC.cs | 25 +++++++++++++++---- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/Renci.SshClient/Renci.SshNet/ConnectionInfo.cs b/Renci.SshClient/Renci.SshNet/ConnectionInfo.cs index 61f02d1a..e8f33ce4 100644 --- a/Renci.SshClient/Renci.SshNet/ConnectionInfo.cs +++ b/Renci.SshClient/Renci.SshNet/ConnectionInfo.cs @@ -304,15 +304,15 @@ namespace Renci.SshNet { {"hmac-md5", (key) => { return new HMac(key.Take(16).ToArray());}}, {"hmac-sha1", (key) => { return new HMac(key.Take(20).ToArray());}}, - //{"hmac-sha2-256", typeof(...)}, - //{"hmac-sha2-256-96", typeof(...)}, + {"hmac-sha2-256", (key) => { return new HMac(key.Take(32).ToArray());}}, + {"hmac-sha2-256-96", (key) => { return new HMac(key.Take(32).ToArray(), 96);}}, //{"hmac-sha2-512", typeof(...)}, //{"hmac-sha2-512-96", typeof(...)}, //{"umac-64@openssh.com", typeof(HMacSha1)}, {"hmac-ripemd160", (key) => { return new HMac(key.Take(20).ToArray());}}, - {"hmac-ripemd160@openssh.com", (key) => { return new HMac(key.Take(20).ToArray());}}, - //{"hmac-md5-96", (key) => { return new HMac(key.Take(12).ToArray());}}, - //{"hmac-sha1-96", (key) => { return new HMac(key.Take(12).ToArray());}}, + {"hmac-ripemd160@openssh.com", (key) => { return new HMac(key.Take(20).ToArray());}}, + {"hmac-md5-96", (key) => { return new HMac(key.Take(16).ToArray(), 96);}}, + {"hmac-sha1-96", (key) => { return new HMac(key.Take(20).ToArray(), 96);}}, //{"none", typeof(...)}, }; diff --git a/Renci.SshClient/Renci.SshNet/Security/Cryptography/HMAC.cs b/Renci.SshClient/Renci.SshNet/Security/Cryptography/HMAC.cs index 417b48c1..c8f15887 100644 --- a/Renci.SshClient/Renci.SshNet/Security/Cryptography/HMAC.cs +++ b/Renci.SshClient/Renci.SshNet/Security/Cryptography/HMAC.cs @@ -32,15 +32,29 @@ namespace Renci.SshNet.Security.Cryptography } } + private HMac() + { + // Create the hash algorithms. + this._hash = new T(); + } + /// /// Rfc 2104. /// /// The key. - public HMac(byte[] key) + public HMac(byte[] key, int hashSizeValue) + : this() { - // Create the hash algorithms. - this._hash = new T(); + this.HashSizeValue = hashSizeValue; + this._key = key; + + this.InternalInitialize(); + } + + public HMac(byte[] key) + : this() + { this.HashSizeValue = this._hash.HashSize; this._key = key; @@ -48,6 +62,7 @@ namespace Renci.SshNet.Security.Cryptography this.InternalInitialize(); } + /// /// Gets or sets the key to use in the hash algorithm. /// @@ -115,7 +130,7 @@ namespace Renci.SshNet.Security.Cryptography this._isHashing = false; - return this._hash.Hash; + return this._hash.Hash.Take(12).ToArray(); } private void InternalInitialize() @@ -145,7 +160,7 @@ namespace Renci.SshNet.Security.Cryptography // Compute inner and outer padding. int i = 0; - for (i = 0; i < 64; i++) + for (i = 0; i < this.BlockSize; i++) { this._innerPadding[i] = 0x36; this._outerPadding[i] = 0x5C;