File: Digest.cs

package info (click to toggle)
dlr-languages 20090805%2Bgit.e6b28d27%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 51,484 kB
  • ctags: 59,257
  • sloc: cs: 298,829; ruby: 159,643; xml: 19,872; python: 2,820; yacc: 1,960; makefile: 96; sh: 65
file content (252 lines) | stat: -rw-r--r-- 10,279 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

using System;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using IronRuby.Builtins;
using IronRuby.Runtime;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
using Microsoft.Scripting.Generation;

namespace IronRuby.StandardLibrary.Digest {

    [RubyModule("Digest")]
    public static class Digest {

        #region Module Methods

        [RubyMethod("const_missing", RubyMethodAttributes.PublicSingleton)]
        public static object ConstantMissing(RubyModule/*!*/ self, [DefaultProtocol, NotNull]string/*!*/ name) {
            // TODO:
            throw new NotImplementedException();
        }

        [RubyMethod("hexencode", RubyMethodAttributes.PublicSingleton)]
        public static MutableString/*!*/ HexEncode(RubyModule/*!*/ self, [NotNull]MutableString/*!*/ str) {
            // TODO:
            throw new NotImplementedException();
        }

        #endregion

        // TODO: MRI doesn't define MD5 constant here, it implements const_missing
#if !SILVERLIGHT
        [RubyClass("MD5", BuildConfig = "!SILVERLIGHT")]
        public class MD5 : Base {
            public MD5()
                : base(System.Security.Cryptography.MD5.Create()) {
            }
        }

        [RubyClass("SHA1", BuildConfig = "!SILVERLIGHT")]
        public class SHA1 : Base {
            public SHA1()
                : base(System.Security.Cryptography.SHA1.Create()) {
            }
        }

        [RubyClass("SHA256", BuildConfig = "!SILVERLIGHT")]
        public class SHA256 : Base {
            public SHA256()
                : base(System.Security.Cryptography.SHA256.Create()) {
            }
        }

        [RubyClass("SHA384", BuildConfig = "!SILVERLIGHT")]
        public class SHA384 : Base {
            public SHA384()
                : base(System.Security.Cryptography.SHA384.Create()) {
            }
        }

        [RubyClass("SHA512", BuildConfig = "!SILVERLIGHT")]
        public class SHA512 : Base {
            public SHA512()
                : base(System.Security.Cryptography.SHA512.Create()) {
            }
        }
#endif

        [RubyClass("Base")]
        public class Base : Class {
            private readonly HashAlgorithm/*!*/ _algorithm;
            private MutableString/*!*/ _buffer;

            protected Base(HashAlgorithm/*!*/ algorithm) {
                Assert.NotNull(algorithm);
                _algorithm = algorithm;
                _buffer = MutableString.CreateBinary();
            }

            [RubyMethod("<<")]
            [RubyMethod("update")]
            public static Base/*!*/ Update(RubyContext/*!*/ context, Base/*!*/ self, MutableString str) {
                self._buffer.Append(str);
                return self;
            }

            [RubyMethod("finish", RubyMethodAttributes.PrivateInstance)]
            public static MutableString/*!*/ Finish(RubyContext/*!*/ context, Base/*!*/ self) {
                byte[] input = self._buffer.ConvertToBytes();
                byte[] hash = self._algorithm.ComputeHash(input);
                return MutableString.CreateBinary(hash);
            }

            [RubyMethod("reset")]
            public static Base/*!*/ Reset(RubyContext/*!*/ context, Base/*!*/ self) {
                self._buffer = MutableString.CreateBinary();
                self._algorithm.Initialize();
                return self;
            }
        }

        [RubyClass("Class"), Includes(typeof(Instance))]
        public class Class {

            [RubyMethod("digest", RubyMethodAttributes.PublicSingleton)]
            public static MutableString Digest(
                CallSiteStorage<Func<CallSite, RubyClass, object>>/*!*/ allocateStorage,
                CallSiteStorage<Func<CallSite, object, MutableString, object>>/*!*/ digestStorage,
                RubyClass/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ str) {

                // TODO: new?
                var allocateSite = allocateStorage.GetCallSite("allocate", 0);
                object obj = allocateSite.Target(allocateSite, self);

                // TODO: check obj
                var site = digestStorage.GetCallSite("digest", 1);
                return (MutableString)site.Target(site, obj, str);
            }

            [RubyMethod("digest", RubyMethodAttributes.PublicSingleton)]
            public static MutableString Digest(RubyClass/*!*/ self) {
                throw RubyExceptions.CreateArgumentError("no data given");
            }

            [RubyMethod("hexdigest", RubyMethodAttributes.PublicSingleton)]
            public static MutableString/*!*/ HexDigest(CallSiteStorage<Func<CallSite, object, MutableString, object>>/*!*/ storage, 
                RubyClass/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ str) {

                var site = storage.GetCallSite("digest", 1);
                MutableString result = (MutableString)site.Target(site, self, str);
                // TODO: check result != null
                return HexEncode(result);
            }

            [RubyMethod("hexdigest", RubyMethodAttributes.PublicSingleton)]
            public static MutableString HexDigest(RubyClass/*!*/ self) {
                throw RubyExceptions.CreateArgumentError("no data given");
            }

            #region Helpers

            internal static MutableString/*!*/ Bytes2Hex(byte[]/*!*/ bytes) {
                return MutableString.Create(System.BitConverter.ToString(bytes).Replace("-", "").ToLower());
            }

            internal static MutableString/*!*/ HexEncode(MutableString/*!*/ str) {
                return Bytes2Hex(str.ConvertToBytes());
            }

            #endregion
        }

        [RubyModule("Instance")]
        public class Instance {

            [RubyMethod("digest")]
            public static MutableString Digest(
                CallSiteStorage<Func<CallSite, object, object, object>>/*!*/ initializeCopyStorage,
                CallSiteStorage<Func<CallSite, RubyClass, object>>/*!*/ allocateStorage,
                CallSiteStorage<Func<CallSite, object, object>>/*!*/ finishStorage,
                object self) {

                object clone;
                if (!RubyUtils.TryDuplicateObject(initializeCopyStorage, allocateStorage, self, true, out clone)) {
                    throw RubyExceptions.CreateArgumentError("unable to copy object");
                }

                var finish = finishStorage.GetCallSite("finish", 0);
                // TODO: cast?
                return (MutableString)finish.Target(finish, clone);
            }

            [RubyMethod("digest")]
            public static MutableString Digest(
                CallSiteStorage<Func<CallSite, object, MutableString, object>>/*!*/ updateStorage,
                CallSiteStorage<Func<CallSite, object, object>>/*!*/ finishStorage,
                CallSiteStorage<Func<CallSite, object, object>>/*!*/ resetStorage,
                object self, [DefaultProtocol, NotNull]MutableString/*!*/ str) {

                var update = updateStorage.GetCallSite("update", 1);
                update.Target(update, self, str);

                var finish = finishStorage.GetCallSite("finish", 0);
                object value = finish.Target(finish, self);

                var reset = resetStorage.GetCallSite("reset", 0);
                reset.Target(reset, self);

                // TODO: cast?
                return (MutableString)value;
            }

            [RubyMethod("digest!")]
            public static MutableString DigestNew(
                CallSiteStorage<Func<CallSite, object, object>>/*!*/ finishStorage,
                CallSiteStorage<Func<CallSite, object, object>>/*!*/ resetStorage,
                object self) {

                var finish = finishStorage.GetCallSite("finish", 0);
                object value = finish.Target(finish, self);

                var reset = resetStorage.GetCallSite("reset", 0);
                reset.Target(reset, self);

                // TODO: cast?
                return (MutableString)value;
            }

            [RubyMethod("hexdigest")]
            public static MutableString HexDigest(
                CallSiteStorage<Func<CallSite, object, object, object>>/*!*/ initializeCopyStorage,
                CallSiteStorage<Func<CallSite, RubyClass, object>>/*!*/ allocateStorage,
                CallSiteStorage<Func<CallSite, object, object>>/*!*/ finishStorage,
                object self) {

                return Class.HexEncode(Digest(initializeCopyStorage, allocateStorage, finishStorage, self));
            }

            [RubyMethod("hexdigest")]
            public static MutableString HexDigest(
                CallSiteStorage<Func<CallSite, object, MutableString, object>>/*!*/ updateStorage,
                CallSiteStorage<Func<CallSite, object, object>>/*!*/ finishStorage,
                CallSiteStorage<Func<CallSite, object, object>>/*!*/ resetStorage,
                object self, [DefaultProtocol, NotNull]MutableString/*!*/ str) {
                return Class.HexEncode(Digest(updateStorage, finishStorage, resetStorage, self, str));
            }

            [RubyMethod("hexdigest!")]
            public static MutableString HexDigestNew(
                CallSiteStorage<Func<CallSite, object, object>>/*!*/ finishStorage,
                CallSiteStorage<Func<CallSite, object, object>>/*!*/ resetStorage,
                object self) {
                return Class.HexEncode(DigestNew(finishStorage, resetStorage, self));
            }
        }
    }
}