~ubuntu-branches/ubuntu/jaunty/beagle/jaunty-security

« back to all changes in this revision

Viewing changes to beagled/xemail-net/src/HMACMD5.cs

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-05-04 00:31:32 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20080504003132-2tkm5o8moo5952ri
Tags: 0.3.7-2ubuntu1
 * Merge from Debian unstable. (LP: #225746) Remaining Ubuntu changes:
  - debian/control:
    + Rename ice{weasel,dove}-beagle to {mozilla,thunderbird}-beagle and
      and update the dependencies accordingly.
    + Change Maintainer to Ubuntu Mono Team.
  - debian/rules:
    + Install the mozilla-beagle and thunderbird-beagle extensions.
  - ice{dove,weasel}.dirs:
    + Renamed to {mozilla,thunderbird}-beagle.dirs.
    + Fixed paths to point to usr/lib/{firefox,thunderbird}

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Security.Cryptography;
 
3
 
 
4
namespace System.Security.Cryptography
 
5
{
 
6
        /// <summary>
 
7
        /// 
 
8
        /// </summary>
 
9
        public class HMACMD5 : KeyedHashAlgorithm 
 
10
        {
 
11
                private MD5     hash1    = null;
 
12
                private MD5     hash2    = null;
 
13
                private bool    bHashing = false;
 
14
                private byte[]  rgbInner = new byte[64];
 
15
                private byte[]  rgbOuter = new byte[64];
 
16
 
 
17
                /// <summary>
 
18
                /// Rfc 2104.
 
19
                /// </summary>
 
20
                /// <param name="rgbKey"></param>
 
21
                public HMACMD5 (byte[] rgbKey) 
 
22
                {
 
23
                        HashSizeValue = 128;
 
24
                        // Create the hash algorithms.
 
25
                        hash1 = MD5.Create();
 
26
                        hash2 = MD5.Create();
 
27
 
 
28
                        this.Key = rgbKey;
 
29
                }    
 
30
 
 
31
                /// <summary>
 
32
                /// 
 
33
                /// </summary>
 
34
                public override byte[] Key 
 
35
                {
 
36
                        get { return (byte[])KeyValue.Clone(); }
 
37
                        set 
 
38
                        {
 
39
                                if(bHashing){
 
40
                                        throw new Exception("Cannot change key during hash operation");
 
41
                                }
 
42
                                if(value.Length > 64){
 
43
                                        KeyValue = hash1.ComputeHash(value);
 
44
                                        // No need to call Initialize, ComputeHash does it automatically.
 
45
                                }
 
46
                                else{
 
47
                                        KeyValue = (byte[]) value.Clone();
 
48
                                }
 
49
                                // Compute rgbInner and rgbOuter.
 
50
                                int i = 0;
 
51
                                for(i=0;i<64;i++){ 
 
52
                                        rgbInner[i] = 0x36;
 
53
                                        rgbOuter[i] = 0x5C;
 
54
                                }
 
55
                                for(i=0;i<KeyValue.Length;i++){
 
56
                                        rgbInner[i] ^= KeyValue[i];
 
57
                                        rgbOuter[i] ^= KeyValue[i];
 
58
                                }
 
59
                        }
 
60
                }
 
61
 
 
62
                /// <summary>
 
63
                /// 
 
64
                /// </summary>
 
65
                public override void Initialize() 
 
66
                {
 
67
                        hash1.Initialize();
 
68
                        hash2.Initialize();
 
69
                        bHashing = false;
 
70
                }
 
71
 
 
72
                /// <summary>
 
73
                /// 
 
74
                /// </summary>
 
75
                /// <param name="rgb"></param>
 
76
                /// <param name="ib"></param>
 
77
                /// <param name="cb"></param>
 
78
                protected override void HashCore(byte[] rgb,int ib,int cb) 
 
79
                {
 
80
                        if(bHashing == false){
 
81
                                hash1.TransformBlock(rgbInner,0,64,rgbInner, 0);
 
82
                                bHashing = true;                
 
83
                        }
 
84
                        hash1.TransformBlock(rgb,ib,cb,rgb,ib);
 
85
                }
 
86
 
 
87
                /// <summary>
 
88
                /// 
 
89
                /// </summary>
 
90
                /// <returns></returns>
 
91
                protected override byte[] HashFinal() 
 
92
                {
 
93
                        if(bHashing == false){
 
94
                                hash1.TransformBlock(rgbInner,0,64,rgbInner,0);
 
95
                                bHashing = true;                
 
96
                        }
 
97
                        // Finalize the original hash.
 
98
                        hash1.TransformFinalBlock(new byte[0],0,0);
 
99
                        // Write the outer array.
 
100
                        hash2.TransformBlock(rgbOuter,0,64,rgbOuter, 0);
 
101
                        // Write the inner hash and finalize the hash.
 
102
                        hash2.TransformFinalBlock(hash1.Hash,0,hash1.Hash.Length);
 
103
                        bHashing = false;
 
104
                        return hash2.Hash;
 
105
                } 
 
106
        }
 
107
}