All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.milagro.amcl.SHA3 Maven / Gradle / Ivy

The newest version!
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
*/

/*
 * Implementation of the Secure Hashing Algorithm SHA-3

 * Generates a message digest. It should be impossible to come
 * come up with two messages that hash to the same value ("collision free").
 *
 * For use with byte-oriented messages only. 
 */


package org.apache.milagro.amcl;

public class SHA3 {
	private long length;
	private int rate,len;
	private long[][] S=new long[5][5];

/* Constructor */
	public SHA3(int olen)
	{
		init(olen);
	}

	public static final int HASH224=28; 
	public static final int HASH256=32;
	public static final int HASH384=48;
	public static final int HASH512=64;

	public static final int SHAKE128=16; 
	public static final int SHAKE256=32; 

	public static final long[] RC={
		0x0000000000000001L,0x0000000000008082L,0x800000000000808AL,0x8000000080008000L,
		0x000000000000808BL,0x0000000080000001L,0x8000000080008081L,0x8000000000008009L,
		0x000000000000008AL,0x0000000000000088L,0x0000000080008009L,0x000000008000000AL,
		0x000000008000808BL,0x800000000000008BL,0x8000000000008089L,0x8000000000008003L,
		0x8000000000008002L,0x8000000000000080L,0x000000000000800AL,0x800000008000000AL,
		0x8000000080008081L,0x8000000000008080L,0x0000000080000001L,0x8000000080008008L};

	private static final int ROUNDS=24;


	private static long rotl(long x,int n)
	{
		return (((x)<>>(64-n)));
	}

	private void transform()
	{ /* basic transformation step */
		int i,j,k;
		long[] C=new long[5];
		long[] D=new long[5];
		long[][] B=new long[5][5];

		for (k=0;k=olen || (m%rate)==0) {done=true; break;} 
						el>>>=8;
					}
					if (done) break;
				}
				if (done) break;
			}
			if (m>=olen) break;
			done=false;
			transform();
		}
		return buff;
	}

	public void hash(byte[] digest)
	{ /* generate a SHA3 hash of appropriate size */
		int q=rate-(int)(length%rate);
		if (q==1) process(0x86); 
		else
		{
			process(0x06);   /* 0x06 for SHA-3 */
			while (length%rate!=rate-1) process(0x00);
			process(0x80); /* this will force a final transform */
		}
		squeeze(digest,len);
	}

	public void shake(byte[] digest,int olen)
	{ /* SHAKE out a buffer of variable length olen */
		int q=rate-(int)(length%rate);
		if (q==1) process(0x9f); 
		else
		{
			process(0x1f);   // 0x06 for SHA-3 !!!!
			while (length%rate!=rate-1) process(0x00);
			process(0x80); /* this will force a final transform */
		}
		squeeze(digest,olen);
	}

/* test program: should produce digests */

//916f6061fe879741ca6469b43971dfdb28b1a32dc36cb3254e812be27aad1d18
//afebb2ef542e6579c50cad06d2e578f9f8dd6881d7dc824d26360feebf18a4fa73e3261122948efcfd492e74e82e2189ed0fb440d187f382270cb455f21dd185
//98be04516c04cc73593fef3ed0352ea9f6443942d6950e29a372a681c3deaf4535423709b02843948684e029010badcc0acd8303fc85fdad3eabf4f78cae165635f57afd28810fc2

/*
	public static void main(String[] args) {

		byte[] test="abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".getBytes();
		byte[] digest=new byte[100];
		int i;

		SHA3 sh256=new SHA3(SHA3.HASH256);
		for (i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy