KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > RubyDigest


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2006, 2007 Ola Bini <ola@ologix.com>
15  * Copyright (C) 2007 Nick Sieger <nicksieger@gmail.com>
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either of the GNU General Public License Version 2 or later (the "GPL"),
19  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the CPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the CPL, the GPL or the LGPL.
28  ***** END LICENSE BLOCK *****/

29 package org.jruby;
30
31 import java.security.MessageDigest JavaDoc;
32 import java.security.NoSuchAlgorithmException JavaDoc;
33 import java.security.NoSuchProviderException JavaDoc;
34
35 import org.jruby.runtime.Block;
36 import org.jruby.runtime.CallbackFactory;
37 import org.jruby.runtime.ObjectAllocator;
38 import org.jruby.runtime.builtin.IRubyObject;
39 import org.jruby.util.ByteList;
40
41 /**
42  * @author <a HREF="mailto:ola.bini@ki.se">Ola Bini</a>
43  */

44 public class RubyDigest {
45     private static String JavaDoc provider = null;
46
47     public static void createDigest(Ruby runtime) {
48         try {
49             Class JavaDoc c = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
50             java.security.Security.insertProviderAt((java.security.Provider JavaDoc)c.newInstance(),2);
51             provider = "BC";
52         } catch(Exception JavaDoc e) {
53             provider = null;
54         }
55
56         RubyModule mDigest = runtime.defineModule("Digest");
57         RubyClass cDigestBase = mDigest.defineClassUnder("Base",runtime.getObject(), Base.BASE_ALLOCATOR);
58
59         CallbackFactory basecb = runtime.callbackFactory(Base.class);
60         
61         cDigestBase.getMetaClass().defineFastMethod("digest",basecb.getFastSingletonMethod("s_digest",RubyKernel.IRUBY_OBJECT));
62         cDigestBase.getMetaClass().defineFastMethod("hexdigest",basecb.getFastSingletonMethod("s_hexdigest",RubyKernel.IRUBY_OBJECT));
63
64         cDigestBase.defineMethod("initialize",basecb.getOptMethod("initialize"));
65         cDigestBase.defineFastMethod("initialize_copy",basecb.getFastMethod("initialize_copy",RubyKernel.IRUBY_OBJECT));
66         cDigestBase.defineFastMethod("update",basecb.getFastMethod("update",RubyKernel.IRUBY_OBJECT));
67         cDigestBase.defineFastMethod("<<",basecb.getFastMethod("update",RubyKernel.IRUBY_OBJECT));
68         cDigestBase.defineFastMethod("digest",basecb.getFastMethod("digest"));
69         cDigestBase.defineFastMethod("hexdigest",basecb.getFastMethod("hexdigest"));
70         cDigestBase.defineFastMethod("to_s",basecb.getFastMethod("hexdigest"));
71         cDigestBase.defineFastMethod("==",basecb.getFastMethod("eq",RubyKernel.IRUBY_OBJECT));
72     }
73
74     public static void createDigestMD5(Ruby runtime) {
75         runtime.getLoadService().require("digest.so");
76         RubyModule mDigest = runtime.getModule("Digest");
77         RubyClass cDigestBase = mDigest.getClass("Base");
78         RubyClass cDigest_MD5 = mDigest.defineClassUnder("MD5",cDigestBase,cDigestBase.getAllocator());
79         cDigest_MD5.setClassVar("metadata",runtime.newString("MD5"));
80     }
81
82     public static void createDigestRMD160(Ruby runtime) {
83         runtime.getLoadService().require("digest.so");
84
85         if(provider == null) {
86             throw runtime.newLoadError("RMD160 not supported without BouncyCastle");
87         }
88
89         RubyModule mDigest = runtime.getModule("Digest");
90         RubyClass cDigestBase = mDigest.getClass("Base");
91         RubyClass cDigest_RMD160 = mDigest.defineClassUnder("RMD160",cDigestBase,cDigestBase.getAllocator());
92         cDigest_RMD160.setClassVar("metadata",runtime.newString("RIPEMD160"));
93     }
94
95     public static void createDigestSHA1(Ruby runtime) {
96         runtime.getLoadService().require("digest.so");
97         RubyModule mDigest = runtime.getModule("Digest");
98         RubyClass cDigestBase = mDigest.getClass("Base");
99         RubyClass cDigest_SHA1 = mDigest.defineClassUnder("SHA1",cDigestBase,cDigestBase.getAllocator());
100         cDigest_SHA1.setClassVar("metadata",runtime.newString("SHA1"));
101     }
102
103     public static void createDigestSHA2(Ruby runtime) {
104         runtime.getLoadService().require("digest.so");
105
106         try {
107             if(provider == null) {
108                 MessageDigest.getInstance("SHA-256");
109             } else {
110                 MessageDigest.getInstance("SHA-256",provider);
111             }
112         } catch(NoSuchAlgorithmException JavaDoc e) {
113             throw runtime.newLoadError("SHA2 not supported");
114         } catch(NoSuchProviderException JavaDoc e) {
115             throw runtime.newLoadError("SHA2 not supported");
116         }
117
118         RubyModule mDigest = runtime.getModule("Digest");
119         RubyClass cDigestBase = mDigest.getClass("Base");
120         RubyClass cDigest_SHA2_256 = mDigest.defineClassUnder("SHA256",cDigestBase,cDigestBase.getAllocator());
121         cDigest_SHA2_256.setClassVar("metadata",runtime.newString("SHA-256"));
122         RubyClass cDigest_SHA2_384 = mDigest.defineClassUnder("SHA384",cDigestBase,cDigestBase.getAllocator());
123         cDigest_SHA2_384.setClassVar("metadata",runtime.newString("SHA-384"));
124         RubyClass cDigest_SHA2_512 = mDigest.defineClassUnder("SHA512",cDigestBase,cDigestBase.getAllocator());
125         cDigest_SHA2_512.setClassVar("metadata",runtime.newString("SHA-512"));
126     }
127
128     public static class Base extends RubyObject {
129         protected static ObjectAllocator BASE_ALLOCATOR = new ObjectAllocator() {
130             public IRubyObject allocate(Ruby runtime, RubyClass klass) {
131                 return new Base(runtime, klass);
132             }
133         };
134         public static IRubyObject s_digest(IRubyObject recv, IRubyObject str) {
135             String JavaDoc name = ((RubyClass)recv).getClassVar("metadata").toString();
136             try {
137                 MessageDigest JavaDoc md = provider == null ? MessageDigest.getInstance(name) : MessageDigest.getInstance(name, provider);
138                 return RubyString.newString(recv.getRuntime(), md.digest(str.convertToString().getBytes()));
139             } catch(NoSuchAlgorithmException JavaDoc e) {
140                 throw recv.getRuntime().newNotImplementedError("Unsupported digest algorithm (" + name + ")");
141             } catch(NoSuchProviderException JavaDoc e) {
142                 throw recv.getRuntime().newNotImplementedError("Unsupported digest algorithm (" + name + ")");
143             }
144         }
145         public static IRubyObject s_hexdigest(IRubyObject recv, IRubyObject str) {
146             String JavaDoc name = ((RubyClass)recv).getClassVar("metadata").toString();
147             try {
148                 MessageDigest JavaDoc md = provider == null ? MessageDigest.getInstance(name) : MessageDigest.getInstance(name, provider);
149                 return RubyString.newString(recv.getRuntime(), ByteList.plain(toHex(md.digest(str.convertToString().getBytes()))));
150             } catch(NoSuchAlgorithmException JavaDoc e) {
151                 throw recv.getRuntime().newNotImplementedError("Unsupported digest algorithm (" + name + ")");
152             } catch(NoSuchProviderException JavaDoc e) {
153                 throw recv.getRuntime().newNotImplementedError("Unsupported digest algorithm (" + name + ")");
154             }
155         }
156
157         private MessageDigest JavaDoc algo;
158         private StringBuffer JavaDoc data;
159
160         public Base(Ruby runtime, RubyClass type) {
161             super(runtime,type);
162             data = new StringBuffer JavaDoc();
163
164             if(type == runtime.getModule("Digest").getClass("Base")) {
165                 throw runtime.newNotImplementedError("Digest::Base is an abstract class");
166             }
167             if(!type.isClassVarDefined("metadata")) {
168                 throw runtime.newNotImplementedError("the " + type + "() function is unimplemented on this machine");
169             }
170             try {
171                 setAlgorithm(type.getClassVar("metadata"));
172             } catch(NoSuchAlgorithmException JavaDoc e) {
173                 throw runtime.newNotImplementedError("the " + type + "() function is unimplemented on this machine");
174             } catch(NoSuchProviderException JavaDoc e) {
175                 throw runtime.newNotImplementedError("the " + type + "() function is unimplemented on this machine");
176             }
177
178         }
179         
180         public IRubyObject initialize(IRubyObject[] args, Block unusedBlock) {
181             if(args.length > 0 && !args[0].isNil()) {
182                 update(args[0]);
183             }
184             return this;
185         }
186
187         public IRubyObject initialize_copy(IRubyObject obj) {
188             if(this == obj) {
189                 return this;
190             }
191             ((RubyObject)obj).checkFrozen();
192
193             data = new StringBuffer JavaDoc(((Base)obj).data.toString());
194             String JavaDoc name = ((Base)obj).algo.getAlgorithm();
195             try {
196                 algo = provider == null ? MessageDigest.getInstance(name) : MessageDigest.getInstance(name, provider);
197             } catch(NoSuchAlgorithmException JavaDoc e) {
198                 throw getRuntime().newNotImplementedError("Unsupported digest algorithm (" + name + ")");
199             } catch(NoSuchProviderException JavaDoc e) {
200                 throw getRuntime().newNotImplementedError("Unsupported digest algorithm (" + name + ")");
201             }
202              return this;
203         }
204
205         public IRubyObject update(IRubyObject obj) {
206             data.append(obj);
207             return this;
208         }
209
210         public IRubyObject digest() {
211             algo.reset();
212             return RubyString.newString(getRuntime(), algo.digest(ByteList.plain(data)));
213         }
214
215         public IRubyObject hexdigest() {
216             algo.reset();
217             return RubyString.newString(getRuntime(), ByteList.plain(toHex(algo.digest(ByteList.plain(data)))));
218         }
219
220         public IRubyObject eq(IRubyObject oth) {
221             boolean ret = this == oth;
222             if(!ret && oth instanceof Base) {
223                 Base b = (Base)oth;
224                 ret = this.algo.getAlgorithm().equals(b.algo.getAlgorithm()) &&
225                     this.digest().equals(b.digest());
226             }
227
228             return ret ? getRuntime().getTrue() : getRuntime().getFalse();
229         }
230
231        private void setAlgorithm(IRubyObject algo) throws NoSuchAlgorithmException JavaDoc, NoSuchProviderException JavaDoc {
232            this.algo = provider == null ? MessageDigest.getInstance(algo.toString()) : MessageDigest.getInstance(algo.toString(),provider);
233         }
234
235         private static String JavaDoc toHex(byte[] val) {
236             StringBuffer JavaDoc out = new StringBuffer JavaDoc();
237             for(int i=0,j=val.length;i<j;i++) {
238                 String JavaDoc ve = Integer.toString((((int)((char)val[i])) & 0xFF),16);
239                 if(ve.length() == 1) {
240                     ve = "0" + ve;
241                 }
242                 out.append(ve);
243             }
244             return out.toString();
245         }
246     }
247 }// RubyDigest
248
Popular Tags