Package anvil.core.crypto

Source Code of anvil.core.crypto.CryptoModule

/*
* $Id: CryptoModule.java,v 1.23 2002/09/16 08:05:02 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.crypto;

import anvil.core.Any;
import anvil.core.AnyString;
import anvil.script.Context;
import com.sun.crypto.provider.SunJCE;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKey;
import java.security.Provider;

///
/// @module CryptoModule
/// Digest and MAC codes, encryption.
///
/// <h2>Digest and MAC codes</h2>
/// <p>
///
/// Anvil crypto library can create message hash codes or checksums from any
/// data. Such codes are useful, for example, in payment applications,
/// where the checksum verifies that data has not been changed. Also the
/// hash codes taken from file contents are useful for checking if the
/// file content is changed or not.
///
/// <ul>
/// <li>
/// <b>Message Digest</b> provides applications the functionality of
/// a message digest algorithm, such as MD5 or SHA. Message
/// digests are secure one-way hash functions that take arbitrary-sized
/// data and output a fixed-length hash value.
///
/// <li>
/// <b>Message Authentication Code (MAC)</b><br>
/// Since everyone can generate the message digest,
/// it may not be suitable for some security related
/// applications. Because of this, Anvil also
/// supports HMAC (rfc2104), which is a mechanism for
/// message authentication using a (secret) key.
/// So you can use a key with a hash algorithm to
/// produce hashes that can only be verified using the same key.
/// </ul>
/// <p>
/// Example. Compute the MD5 digest and hmac and print them out as hex.<br>
/// <pre>
/// key = "Jefe";
/// data = "what do ya want for nothing?";
/// digest = crypto.MD5().update(data).final().toHex();
/// hmac = crypto.MD5(key).update(data).final().toHex();
/// print "The digest is "+digest;
/// print "The hmac is "+hmac;
/// </pre>
/// This will produce:
/// <pre>
/// The digest is d03cb659cbf9192dcd066272249f8412
/// The hmac is 750c783e6ab0b503eaa86e310a5db738
/// </pre>
///
/// <h2>Encryption</h2>
/// <p>
/// Anvil crypto library can also encrypt and decrypt data using DES, TripleDes and
/// Blowfish algorithms.
///
/// <p>
/// Example. Encrypt and decrypt a data string using DES.<br>
/// <pre>
/// key = "Jefe1234";
/// data1 = "what do ya want ";
/// data2 = "for nothing?";
/// cipher = crypto.encrypt(crypto.DES, key);
/// crypted = cipher.update(data1);
/// crypted = crypted.concat(cipher.update(data2));
/// crypted = crypted.concat(cipher.final());
/// recovered = crypto.decrypt(crypto.DES, key).final(crypted);
/// </pre>
///
/// @author: Jaripekka Salminen
///
public class CryptoModule
{

  private static boolean initialized= false;

  /// @const DES DES algorithm
  public static final Any DES = new AnyString("DES");

  /// @const TRIPLE_DES 3DES algorithm
  public static final Any TRIPLE_DES = new AnyString("DESede");

  /// @const BLOWFISH Blowfish algorithm
  public static final Any BLOWFISH = new AnyString("Blowfish");



  /// @function encrypt
  /// @synopsis Cipher encrypt(string algorithm, object key)
  /// @param algorithm should be one of supported algoritmhs such as
  ///   crypto.DES, crypto.TRIPLE_DES, crypto.BLOWFISH.
  /// @param key string or binary key
  /// @return a cipher object, which takes data and
  ///   then returns the encrypted data.
  public static final Object[] p_encrypt = { null, "algorithm", "key" };
  public static final Any encrypt(Context context, String algrorithm, Any key)
  {
    init();
    try {
      return new AnyCipher(algrorithm, Cipher.ENCRYPT_MODE, key);
    } catch (Exception e) {
      throw context.exception(e);
    }
  }


  /// @function decrypt
  /// @synopsis Cipher decrypt(string algorithm, object key)
  /// @param algorithm should be one of supported algoritmhs such as
  ///   crypto.DES, crypto.TRIPLE_DES, crypto.BLOWFISH.
  /// @param key string or binary key
  /// @return a cipher object, which takes encrypted data and
  ///   then returns the original data.
  public static final Object[] p_decrypt = { null, "algrorithm", "key" };
  public static final Any decrypt(Context context, String algorithm, Any key)
  {
    init();
    try {
      return new AnyCipher(algorithm, Cipher.DECRYPT_MODE, key);
    } catch (Exception e) {
      throw context.exception(e);
    }
  }


  static void init() {
    if (initialized) {
      return;
    }
    /* Installs SunJCE provider */
    Provider sunJce = new com.sun.crypto.provider.SunJCE();
    Security.addProvider(sunJce);
    initialized = true;
  }
 
 
  public static final anvil.script.compiler.NativeNamespace __module__ =
    new anvil.script.compiler.NativeNamespace(
      "crypto",
      CryptoModule.class,
      new String[] {
        "anvil.core.crypto.AnyCipher",
        "anvil.core.crypto.AnyMessageHash",
        "anvil.core.crypto.AnyMD5",
        "anvil.core.crypto.AnySHA",
      },
     //DOC{{
    ""+
      "\n" +
      " @module CryptoModule\n" +
      " Digest and MAC codes, encryption.\n" +
      " \n" +
      " <h2>Digest and MAC codes</h2>\n" +
      " <p>\n" +
      " \n" +
      " Anvil crypto library can create message hash codes or checksums from any\n" +
      " data. Such codes are useful, for example, in payment applications,\n" +
      " where the checksum verifies that data has not been changed. Also the\n" +
      " hash codes taken from file contents are useful for checking if the\n" +
      " file content is changed or not.\n" +
      " \n" +
      " <ul>\n" +
      " <li>\n" +
      " <b>Message Digest</b> provides applications the functionality of\n" +
      " a message digest algorithm, such as MD5 or SHA. Message\n" +
      " digests are secure one-way hash functions that take arbitrary-sized\n" +
      " data and output a fixed-length hash value.\n" +
      " \n" +
      " <li>\n" +
      " <b>Message Authentication Code (MAC)</b><br>\n" +
      " Since everyone can generate the message digest,\n" +
      " it may not be suitable for some security related\n" +
      " applications. Because of this, Anvil also\n" +
      " supports HMAC (rfc2104), which is a mechanism for\n" +
      " message authentication using a (secret) key.\n" +
      " So you can use a key with a hash algorithm to\n" +
      " produce hashes that can only be verified using the same key.\n" +
      " </ul>\n" +
      " <p>\n" +
      " Example. Compute the MD5 digest and hmac and print them out as hex.<br>\n" +
      " <pre>\n" +
      " key = \"Jefe\";\n" +
      " data = \"what do ya want for nothing?\";\n" +
      " digest = crypto.MD5().update(data).final().toHex();\n" +
      " hmac = crypto.MD5(key).update(data).final().toHex();\n" +
      " print \"The digest is \"+digest;\n" +
      " print \"The hmac is \"+hmac;\n" +
      " </pre>\n" +
      " This will produce:\n" +
      " <pre>\n" +
      " The digest is d03cb659cbf9192dcd066272249f8412\n" +
      " The hmac is 750c783e6ab0b503eaa86e310a5db738\n" +
      " </pre>\n" +
      " \n" +
      " <h2>Encryption</h2>\n" +
      " <p>\n" +
      " Anvil crypto library can also encrypt and decrypt data using DES, TripleDes and\n" +
      " Blowfish algorithms.\n" +
      " \n" +
      " <p>\n" +
      " Example. Encrypt and decrypt a data string using DES.<br>\n" +
      " <pre>\n" +
      " key = \"Jefe1234\";\n" +
      " data1 = \"what do ya want \";\n" +
      " data2 = \"for nothing?\";\n" +
      " cipher = crypto.encrypt(crypto.DES, key);\n" +
      " crypted = cipher.update(data1);\n" +
      " crypted = crypted.concat(cipher.update(data2));\n" +
      " crypted = crypted.concat(cipher.final());\n" +
      " recovered = crypto.decrypt(crypto.DES, key).final(crypted);\n" +
      " </pre>\n" +
      "\n" +
      " @author: Jaripekka Salminen\n" +
      "\n" +
      " @const DES DES algorithm\n" +
      " @const TRIPLE_DES 3DES algorithm\n" +
      " @const BLOWFISH Blowfish algorithm\n" +
      " @function encrypt\n" +
      " @synopsis Cipher encrypt(string algorithm, object key) \n" +
      " @param algorithm should be one of supported algoritmhs such as\n" +
      "   crypto.DES, crypto.TRIPLE_DES, crypto.BLOWFISH.\n" +
      " @param key string or binary key\n" +
      " @return a cipher object, which takes data and\n" +
      "   then returns the encrypted data.\n" +
      " @function decrypt\n" +
      " @synopsis Cipher decrypt(string algorithm, object key) \n" +
      " @param algorithm should be one of supported algoritmhs such as\n" +
      "   crypto.DES, crypto.TRIPLE_DES, crypto.BLOWFISH.\n" +
      " @param key string or binary key\n" +
      " @return a cipher object, which takes encrypted data and\n" +
      "   then returns the original data.\n"
    //}}DOC
    );
 
 
}
TOP

Related Classes of anvil.core.crypto.CryptoModule

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.