001: /*
002: * $Id: AnySHA.java,v 1.5 2002/09/16 08:05:02 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.core.crypto;
011:
012: import anvil.core.Any;
013: import anvil.script.Context;
014: import java.security.InvalidKeyException;
015: import java.security.MessageDigest;
016: import java.security.NoSuchAlgorithmException;
017: import javax.crypto.Mac;
018: import javax.crypto.SecretKey;
019: import javax.crypto.spec.SecretKeySpec;
020:
021: ///
022: /// @class SHA
023: /// This class is used for creating message hash codes:
024: /// <i>Message Digest</i> or <i>Message Authentication Code (MAC)</i>.
025: ///
026:
027: /**
028: * class SHA
029: *
030: * @author: Jaripekka Salminen
031: */
032: public class AnySHA extends AnyMessageHash {
033:
034: ///
035: /// @constructor SHA
036: /// @synopsis SHA SHA() ; When the SHA algorithm is used without
037: /// any key, it will return a MessageHash datatype, which will make
038: /// a <i>SHA message digest</i> from the data given to it.
039: /// @synopsis SHA SHA(object key) ; When the SHA algorith is given
040: /// a key (string or binary), it will return a MessageHash datatype,
041: /// which will make a <i>HmacSHA1 message authentication code</i>
042: /// from the data given to it.
043: ///
044: /// @param key string or binary key
045: /// @return message hash
046: ///
047: public static final Object[] newInstance = { null, "*key", null };
048:
049: public static final Any newInstance(Context context, Any key) {
050: CryptoModule.init();
051: try {
052: if (key == null) {
053: return new AnyMessageHash("SHA");
054: } else {
055: return new AnyMessageHash("HmacSHA1", key);
056: }
057: } catch (Exception e) {
058: throw context.exception(e);
059: }
060: }
061:
062: public static final anvil.script.compiler.NativeClass __class__ = new anvil.script.compiler.NativeClass(
063: "SHA", AnySHA.class,
064: AnyMessageHash.__class__,
065: //DOC{{
066: ""
067: + "\n"
068: + " @class SHA\n"
069: + " This class is used for creating message hash codes:\n"
070: + " <i>Message Digest</i> or <i>Message Authentication Code (MAC)</i>.\n"
071: + "\n"
072: + "\n"
073: + " @constructor SHA\n"
074: + " @synopsis SHA SHA() ; When the SHA algorithm is used without \n"
075: + " any key, it will return a MessageHash datatype, which will make \n"
076: + " a <i>SHA message digest</i> from the data given to it.\n"
077: + " @synopsis SHA SHA(object key) ; When the SHA algorith is given \n"
078: + " a key (string or binary), it will return a MessageHash datatype, \n"
079: + " which will make a <i>HmacSHA1 message authentication code</i>\n"
080: + " from the data given to it.\n" + " \n"
081: + " @param key string or binary key\n"
082: + " @return message hash\n" + "\n"
083: //}}DOC
084: );
085: static {
086: CryptoModule.class.getName();
087: }
088:
089: /*package*/AnySHA(String algorithm)
090: throws NoSuchAlgorithmException {
091: super (algorithm);
092: }
093:
094: /*package*/AnySHA(String algorithm, Any anyKey)
095: throws NoSuchAlgorithmException, InvalidKeyException {
096: super (algorithm, anyKey);
097: }
098:
099: public anvil.script.ClassType classOf() {
100: return __class__;
101: }
102:
103: }
|