Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/cpl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2006, 2007 Ola Bini <ola@ologix.com> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 package org.jruby.ext.openssl;
 
 
 import org.jruby.Ruby;

Author(s):
Ola Bini
 
 public class HMAC extends RubyObject {
     private static final long serialVersionUID = 7602535792884680307L;
 
     private static ObjectAllocator HMAC_ALLOCATOR = new ObjectAllocator() {
         public IRubyObject allocate(Ruby runtimeRubyClass klass) {
             return new HMAC(runtimeklass);
         }
     };
     
     public static void createHMAC(Ruby runtimeRubyModule ossl) {
         RubyClass cHMAC = ossl.defineClassUnder("HMAC",runtime.getObject(),);
         RubyClass openSSLError = ossl.getClass("OpenSSLError");
         ossl.defineClassUnder("HMACError",openSSLError,openSSLError.getAllocator());
 
         cHMAC.defineAnnotatedMethods(HMAC.class);
     }
 
     static Mac getMac(String algoNamethrows NoSuchAlgorithmException {
         // some algorithms need the - removed; this is ugly, I know.
         try {
             return Mac.getInstance("HMAC" + algoName.replaceAll("-"""));
         } catch (NoSuchAlgorithmException nsae) {
             return Mac.getInstance("HMAC-" + algoName.replaceAll("-"""));
         }
     }
     
     @JRubyMethod(name = "digest", meta = true)
     public static IRubyObject s_digest(IRubyObject recvIRubyObject digestIRubyObject kayIRubyObject data) {
         String algoName = getDigestAlgorithmName(digest);
         try {
             Mac mac = getMac(algoName);
             byte[] key = kay.convertToString().getBytes();
             SecretKey keysp = new SecretKeySpec(keymac.getAlgorithm());
             mac.init(keysp);
             return RubyString.newString(recv.getRuntime(), mac.doFinal(data.convertToString().getBytes()));
         } catch (Exception e) {
             e.printStackTrace();
             throw recv.getRuntime().newNotImplementedError(e.getMessage());
         }
     }
 
     @JRubyMethod(name = "hexdigest", meta = true)
     public static IRubyObject s_hexdigest(IRubyObject recvIRubyObject digestIRubyObject kayIRubyObject data) {
         String algoName = getDigestAlgorithmName(digest);
         try {
             Mac mac = getMac(algoName);
             byte[] key = kay.convertToString().getBytes();
             SecretKey keysp = new SecretKeySpec(keymac.getAlgorithm());
             mac.init(keysp);
             return RubyString.newString(recv.getRuntime(), ByteList.plain(Utils.toHex(mac.doFinal(data.convertToString().getBytes()))));
         } catch (Exception e) {
             throw recv.getRuntime().newNotImplementedError(e.getMessage());
        }
    }
    public HMAC(Ruby runtimeRubyClass type) {
        super(runtime,type);
    }
    private Mac mac;
    private byte[] key;
    private StringBuffer data = new StringBuffer();
    public IRubyObject initialize(IRubyObject kayIRubyObject digest) {
        String algoName = getDigestAlgorithmName(digest);
        try {
             = getMac(algoName);
             = kay.convertToString().getBytes();
            SecretKey keysp = new SecretKeySpec(.getAlgorithm());
            .init(keysp);
        } catch (Exception e) {
            throw getRuntime().newNotImplementedError(e.getMessage());
        }
        return this;
    }
    @Override
    public IRubyObject initialize_copy(IRubyObject obj) {
        if(this == obj) {
            return this;
        }
        checkFrozen();
        String name = ((HMAC)obj)..getAlgorithm();
        try {
             = Mac.getInstance(name);
             = ((HMAC)obj).;
            SecretKey keysp = new SecretKeySpec(,name);
            .init(keysp);
        } catch(Exception e) {
            throw getRuntime().newNotImplementedError("Unsupported MAC algorithm (" + name + ")");
        }
        
         = new StringBuffer(((HMAC)obj)..toString());
        return this;
    }
    @JRubyMethod(name={"update""<<"})
    public IRubyObject update(IRubyObject obj) {
        .append(obj);
        return this;
    }
    public IRubyObject digest() {
        .reset();
        return RubyString.newString(getRuntime(), .doFinal(ByteList.plain()));
    }
    public IRubyObject reset() {
        .setLength(0);
        return this;
    }
    @JRubyMethod(name={"hexdigest","inspect","to_s"})
    public IRubyObject hexdigest() {
        .reset();
        return RubyString.newString(getRuntime(), ByteList.plain(Utils.toHex(.doFinal(ByteList.plain()))));
    }
    String getAlgorithm() {
        return this..getAlgorithm();
    }
    private static String getDigestAlgorithmName(IRubyObject digest) {
        String algoName = null;
        if (digest instanceof Digest) {
            algoName = ((Digestdigest).getShortAlgorithm();
        } else {
            algoName = digest.asString().toString();
        }
        return algoName;
    }
}// HMAC
New to GrepCode? Check out our FAQ X