/*
* $Id: AnyMessageHash.java,v 1.20 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.AnyAbstractClass;
import anvil.core.io.AnyFile;
import anvil.core.net.AnyURL;
import anvil.script.Context;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
///
/// @class MessageHash
/// This class is used for creating message hash codes:
/// <i>Message Digest</i> or <i>Message Authentication Code (MAC)</i>.
///
/**
* class AnyMessageHash
*
* Holds message hash codes
* (Message Digest or Message Authentication Code).
*
* Compatible with Test Cases for HMAC-MD5 and HMAC-SHA-1
* http://www.ietf.org/rfc/rfc2202.txt
*
* @author: Jaripekka Salminen
*/
public class AnyMessageHash extends AnyAbstractClass
{
public static final anvil.script.compiler.NativeClass __class__ =
new anvil.script.compiler.NativeClass("MessageHash", AnyMessageHash.class,
//DOC{{
""+
"\n" +
" @class MessageHash\n" +
" This class is used for creating message hash codes:\n" +
" <i>Message Digest</i> or <i>Message Authentication Code (MAC)</i>.\n" +
"\n" +
" @method update\n" +
" @synopsis void update(object data [ , object data2, ... ]) ;\n" +
" Adds one or more data items (string or binary).\n" +
" @synopsis void update(File data [ , File data2, ...]) ;\n" +
" Adds the contents of one or more data files.\n" +
" @synopsis void update(URL data [, URL data2, ...]) ;\n" +
" Adds the contents from one or more URL:s.\n" +
" @param data one or more data items\n" +
" @method final\n" +
" @synopsis binary final() \n" +
" @return the message hash code.\n" +
" The code can be converted to a hex string using toHex() function.\n"
//}}DOC
);
private MessageDigest _messageDigest;
private Mac _mac;
/**
* @param algorithm "MD5" or "SHA"
*/
/*package*/ AnyMessageHash(String algorithm) throws NoSuchAlgorithmException
{
_messageDigest = MessageDigest.getInstance(algorithm);
}
/**
*
*/
/*package*/ AnyMessageHash(String algorithm, Any anyKey)
throws NoSuchAlgorithmException, InvalidKeyException
{
byte [] key;
int key_length;
if (anyKey.isBinary()) {
key = anyKey.toBinary();
key_length = anyKey.sizeOf();
} else {
key = anvil.util.Conversions.getBytes(anyKey.toString());
key_length = key.length;
}
SecretKey sk = new javax.crypto.spec.SecretKeySpec(key, 0, key_length, algorithm);
_mac = Mac.getInstance(algorithm);
_mac.init(sk);
}
/**
*
*/
public AnyMessageHash update(byte[] data, int offset, int length)
{
if (_messageDigest != null) {
_messageDigest.update(data, offset, length);
} else {
_mac.update(data, offset, length);
}
return this;
}
static final int _BUFSIZE = 2048;
/**
*
*/
private Any update(Context context, File file) throws IOException
{
context.checkRead(file.getPath());
return update(new FileInputStream(file));
}
/**
*
*/
private Any update(URL url) throws IOException
{
return update(url.openStream());
}
/**
*
*/
private Any update(InputStream inputs) throws IOException {
byte[] data = new byte[_BUFSIZE];
BufferedInputStream binputs = new BufferedInputStream(inputs);
while (true) {
int nbytes = binputs.read(data,0,_BUFSIZE);
if (nbytes < 1) {
break;
}
if (_messageDigest != null) {
_messageDigest.update(data,0,nbytes);
} else {
_mac.update(data,0,nbytes);
}
}
binputs.close();
return this;
}
/**
*
*/
public anvil.script.ClassType classOf() {
return __class__;
}
/**
*
*/
public Object toObject()
{
if (_messageDigest != null) {
return _messageDigest;
} else {
return _mac;
}
}
/**
*
*/
/// @method update
/// @synopsis void update(object data [ , object data2, ... ]) ;
/// Adds one or more data items (string or binary).
/// @synopsis void update(File data [ , File data2, ...]) ;
/// Adds the contents of one or more data files.
/// @synopsis void update(URL data [, URL data2, ...]) ;
/// Adds the contents from one or more URL:s.
/// @param data one or more data items
public Any m_update(Context context, Any[] parameters)
{
if (parameters.length > 0) {
try {
for (int i=0; i<parameters.length; i++) {
Any param = parameters[i];
if (param.isBinary()) {
update(param.toBinary(), 0, param.sizeOf());
} else if (param instanceof AnyFile) {
update(context, (File)param.toObject());
} else if (param instanceof AnyURL) {
update((URL)param.toObject());
} else {
byte[] bytes = anvil.util.Conversions.getBytes(param.toString());
update(bytes, 0, bytes.length);
}
}
} catch (IOException e) {
throw context.exception(e);
}
return this;
} else {
throw parametersMissing(context, "update");
}
}
/// @method final
/// @synopsis binary final()
/// @return the message hash code.
/// The code can be converted to a hex string using toHex() function.
public Any m_final(Context context, Any[] parameters)
{
if (_messageDigest != null) {
return Any.create(_messageDigest.digest());
} else {
return Any.create(_mac.doFinal());
}
}
}