-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JCE: add SHA-224 MessageDigest implementation
- Loading branch information
Showing
6 changed files
with
391 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/main/java/com/wolfssl/provider/jce/WolfCryptMessageDigestSha224.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* WolfCryptMessageDigestSha224.java | ||
* | ||
* Copyright (C) 2006-2025 wolfSSL Inc. | ||
* | ||
* This file is part of wolfSSL. | ||
* | ||
* wolfSSL is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* wolfSSL is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA | ||
*/ | ||
|
||
package com.wolfssl.provider.jce; | ||
|
||
import java.security.MessageDigestSpi; | ||
import javax.crypto.ShortBufferException; | ||
|
||
import com.wolfssl.wolfcrypt.Sha224; | ||
|
||
/** | ||
* wolfCrypt JCE SHA2-224 MessageDigest wrapper | ||
*/ | ||
public final class WolfCryptMessageDigestSha224 | ||
extends MessageDigestSpi implements Cloneable { | ||
|
||
/* internal reference to wolfCrypt JNI Sha object */ | ||
private Sha224 sha; | ||
|
||
/** | ||
* Create new WolfCryptMessageDigestSha224 object | ||
*/ | ||
public WolfCryptMessageDigestSha224() { | ||
|
||
sha = new Sha224(); | ||
sha.init(); | ||
} | ||
|
||
/** | ||
* Create new WolfCryptMessageDigestSha224 based on existing Sha224 object. | ||
* Existing object should already be initialized. | ||
* | ||
* @param sha initialized Sha224 object to be used with this MessageDigest | ||
*/ | ||
private WolfCryptMessageDigestSha224(Sha224 sha) { | ||
this.sha = sha; | ||
} | ||
|
||
@Override | ||
protected byte[] engineDigest() { | ||
|
||
byte[] digest = new byte[Sha224.DIGEST_SIZE]; | ||
|
||
try { | ||
|
||
this.sha.digest(digest); | ||
|
||
} catch (ShortBufferException e) { | ||
throw new RuntimeException(e.getMessage()); | ||
} | ||
|
||
log("generated final digest, len: " + digest.length); | ||
|
||
return digest; | ||
} | ||
|
||
@Override | ||
protected void engineReset() { | ||
|
||
this.sha.init(); | ||
|
||
log("engine reset"); | ||
} | ||
|
||
@Override | ||
protected void engineUpdate(byte input) { | ||
|
||
byte[] tmp = new byte[1]; | ||
tmp[0] = input; | ||
|
||
this.sha.update(tmp, 1); | ||
|
||
log("update with single byte"); | ||
} | ||
|
||
@Override | ||
protected void engineUpdate(byte[] input, int offset, int len) { | ||
|
||
this.sha.update(input, offset, len); | ||
|
||
log("update, offset: " + offset + ", len: " + len); | ||
} | ||
|
||
@Override | ||
protected int engineGetDigestLength() { | ||
return this.sha.digestSize(); | ||
} | ||
|
||
private void log(String msg) { | ||
WolfCryptDebug.print("[MessageDigest, SHA224] " + msg); | ||
} | ||
|
||
@Override | ||
public Object clone() { | ||
Sha224 shaCopy = (Sha224)this.sha.clone(); | ||
return new WolfCryptMessageDigestSha224(shaCopy); | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
@Override | ||
protected void finalize() throws Throwable { | ||
try { | ||
if (this.sha != null) | ||
this.sha.releaseNativeStruct(); | ||
} finally { | ||
super.finalize(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.