📄 digest.c
字号:
/* * digest.c * * kaffe.security.provider.MD2 * kaffe.security.provider.MD4 * kaffe.security.provider.MD5 * kaffe.security.provider.SHA * * Copyright (c) 1996, 1997 * Transvirtual Technologies, Inc. All rights reserved. * * See the file "license.terms" for information on usage and redistribution * of this file. */#include "config.h"#include "config-std.h"#include "config-mem.h"#include "classMethod.h"#include "gtypes.h"#include "locks.h"#include "object.h"#include "itypes.h"#include "exception.h"#include "stringSupport.h"#include "fp.h"#include "sha-1.h"#include <native.h>#include "org_kaffe_security_provider_MD2.h"#include "org_kaffe_security_provider_MD4.h"#include "org_kaffe_security_provider_MD5.h"#include "org_kaffe_security_provider_SHA.h"#if (!defined(HAVE_MD2INIT) || !defined(HAVE_MD4INIT)) && !defined(HAVE_LIBMD)static void supportDisabled (JNIEnv* env){ jclass sd = (*env)->FindClass(env, "org.kaffe.util.SupportDisabled"); (*env)->ThrowNew(env, sd, "libmd was not found by Kaffe configure script");}#endif/**************************** MD2 ***********************************/#if defined(HAVE_MD2INIT) || defined(HAVE_LIBMD)#include <md2.h>void JNICALLJava_org_kaffe_security_provider_MD2_Init(JNIEnv *env, jobject this){ const jclass class = (*env)->GetObjectClass(env, this); const jfieldID contextField = (*env)->GetFieldID(env, class, "context", "[B"); jbyteArray ary; MD2_CTX ctx; /* Initialize MD2 context */ MD2Init(&ctx); /* Copy initialized context into byte array */ ary = (*env)->NewByteArray(env, sizeof(ctx)); (*env)->SetByteArrayRegion(env, ary, 0, sizeof(ctx), (jbyte *) &ctx); if ((*env)->ExceptionOccurred(env)) { return; } /* Assign byte array to instance variable */ (*env)->SetObjectField(env, this, contextField, (jobject) ary);}void JNICALLJava_org_kaffe_security_provider_MD2_Update(JNIEnv *env, jobject this, jbyteArray buf, jint off, jint len){ const jclass class = (*env)->GetObjectClass(env, this); const jclass aiobClass = (*env)->FindClass(env, "java/lang/ArrayIndexOutOfBoundsException"); const jfieldID contextField = (*env)->GetFieldID(env, class, "context", "[B"); jbyte *ctxBytes, *bufBytes; jbyteArray ctxArray; /* Get context byte array data */ ctxArray = (*env)->GetObjectField(env, this, contextField); ctxBytes = (*env)->GetByteArrayElements(env, ctxArray, NULL); if ((*env)->ExceptionOccurred(env)) { return; } /* Get data byte array data */ if (off < 0 || off + len > (*env)->GetArrayLength(env, buf)) { (*env)->ThrowNew(env, aiobClass, "out of range"); return; } bufBytes = (*env)->GetByteArrayElements(env, buf, NULL); if ((*env)->ExceptionOccurred(env)) { return; } /* Update with new data and release array data */ MD2Update((MD2_CTX *) ctxBytes, bufBytes + off, len); (*env)->ReleaseByteArrayElements(env, ctxArray, ctxBytes, 0); (*env)->ReleaseByteArrayElements(env, buf, bufBytes, JNI_ABORT);}void JNICALLJava_org_kaffe_security_provider_MD2_Final(JNIEnv *env, jobject this, jbyteArray buf, jint off){ const jclass class = (*env)->GetObjectClass(env, this); const jclass aiobClass = (*env)->FindClass(env, "java/lang/ArrayIndexOutOfBoundsException"); const jfieldID contextField = (*env)->GetFieldID(env, class, "context", "[B"); const jfieldID diglenField = (*env)->GetStaticFieldID(env, class, "DIGEST_LENGTH", "I"); jint digestLen = (*env)->GetStaticIntField(env, this, diglenField); jbyte *ctxBytes, *bufBytes; jbyteArray ctxArray; /* Get context byte array data */ ctxArray = (*env)->GetObjectField(env, this, contextField); ctxBytes = (*env)->GetByteArrayElements(env, ctxArray, NULL); if ((*env)->ExceptionOccurred(env)) { return; } /* Get data byte array data */ if (off < 0 || off + digestLen > (*env)->GetArrayLength(env, buf)) { (*env)->ThrowNew(env, aiobClass, "out of range"); return; } bufBytes = (*env)->GetByteArrayElements(env, buf, NULL); if ((*env)->ExceptionOccurred(env)) { return; } /* Finalize and release byte arrays */ MD2Final(bufBytes + off, (MD2_CTX *) ctxBytes); (*env)->ReleaseByteArrayElements(env, ctxArray, ctxBytes, 0); (*env)->ReleaseByteArrayElements(env, buf, bufBytes, JNI_ABORT);}#elsevoid JNICALLJava_org_kaffe_security_provider_MD2_Init(JNIEnv *env, jobject this UNUSED){ supportDisabled(env);}void JNICALLJava_org_kaffe_security_provider_MD2_Update(JNIEnv *env, jobject this UNUSED, jbyteArray buf UNUSED, jint off UNUSED, jint len UNUSED){ supportDisabled(env);}void JNICALLJava_org_kaffe_security_provider_MD2_Final(JNIEnv *env, jobject this UNUSED, jbyteArray buf UNUSED, jint off UNUSED){ supportDisabled(env);}#endif /* defined(HAVE_MD2INIT) || defined(HAVE_LIBMD) *//**************************** MD4 ***********************************/#if defined(HAVE_MD4INIT) || defined(HAVE_LIBMD)#include <md4.h>void JNICALLJava_org_kaffe_security_provider_MD4_Init(JNIEnv *env, jobject this){ const jclass class = (*env)->GetObjectClass(env, this); const jfieldID contextField = (*env)->GetFieldID(env, class, "context", "[B"); jbyteArray ary; MD4_CTX ctx; /* Initialize MD4 context */ MD4Init(&ctx); /* Copy initialized context into byte array */ ary = (*env)->NewByteArray(env, sizeof(ctx)); (*env)->SetByteArrayRegion(env, ary, 0, sizeof(ctx), (jbyte *) &ctx); if ((*env)->ExceptionOccurred(env)) { return; } /* Assign byte array to instance variable */ (*env)->SetObjectField(env, this, contextField, (jobject) ary);}void JNICALLJava_org_kaffe_security_provider_MD4_Update(JNIEnv *env, jobject this, jbyteArray buf, jint off, jint len){ const jclass class = (*env)->GetObjectClass(env, this); const jclass aiobClass = (*env)->FindClass(env, "java/lang/ArrayIndexOutOfBoundsException"); const jfieldID contextField = (*env)->GetFieldID(env, class, "context", "[B"); jbyte *ctxBytes, *bufBytes; jbyteArray ctxArray; /* Get context byte array data */ ctxArray = (*env)->GetObjectField(env, this, contextField); ctxBytes = (*env)->GetByteArrayElements(env, ctxArray, NULL); if ((*env)->ExceptionOccurred(env)) { return; } /* Get data byte array data */ if (off < 0 || off + len > (*env)->GetArrayLength(env, buf)) { (*env)->ThrowNew(env, aiobClass, "out of range"); return; } bufBytes = (*env)->GetByteArrayElements(env, buf, NULL); if ((*env)->ExceptionOccurred(env)) { return; } /* Update with new data and release array data */ MD4Update((MD4_CTX *) ctxBytes, bufBytes + off, len); (*env)->ReleaseByteArrayElements(env, ctxArray, ctxBytes, 0); (*env)->ReleaseByteArrayElements(env, buf, bufBytes, JNI_ABORT);}void JNICALLJava_org_kaffe_security_provider_MD4_Final(JNIEnv *env, jobject this, jbyteArray buf, jint off){ const jclass class = (*env)->GetObjectClass(env, this); const jclass aiobClass = (*env)->FindClass(env, "java/lang/ArrayIndexOutOfBoundsException"); const jfieldID contextField = (*env)->GetFieldID(env, class, "context", "[B"); const jfieldID diglenField = (*env)->GetStaticFieldID(env, class, "DIGEST_LENGTH", "I"); jint digestLen = (*env)->GetStaticIntField(env, this, diglenField); jbyte *ctxBytes, *bufBytes; jbyteArray ctxArray; /* Get context byte array data */ ctxArray = (*env)->GetObjectField(env, this, contextField); ctxBytes = (*env)->GetByteArrayElements(env, ctxArray, NULL); if ((*env)->ExceptionOccurred(env)) { return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -