alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (MessageDigestAlgorithmBenchmark.java)

This example Java source code file (MessageDigestAlgorithmBenchmark.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

algorithm, assertionerror, beforeexperiment, hashfunction, hashmethod, md5, messagedigest, messagedigestalgorithmbenchmark, override, param, random_seed, security, sha-256, sha_384, string, util

The MessageDigestAlgorithmBenchmark.java Java example source code

/*
 * Copyright (C) 2012 The Guava Authors
 *
 * Licensed 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.
 */

package com.google.common.hash;

import com.google.caliper.BeforeExperiment;
import com.google.caliper.Benchmark;
import com.google.caliper.Param;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * Benchmarks for comparing {@link MessageDigest}s and {@link HashFunction}s that wrap
 * {@link MessageDigest}s.
 *
 * <p>Parameters for the benchmark are:
 * <ul>
 * <li>size: The length of the byte array to hash.
 * <li>algorithm: the algorithm to hash with (e.g. MD5, SHA1, etc.).
 * <li>hashMethod: how to hash the data (using the Hashing API or the MessageDigest API).
 * </ul>
 *
 * @author Kurt Alfred Kluever
 */
public class MessageDigestAlgorithmBenchmark {
  @Param({"10", "1000", "100000", "1000000"}) int size;
  @Param Algorithm algorithm;
  @Param HashMethod hashMethod;

  private enum HashMethod {
    MESSAGE_DIGEST_API() {
      @Override public byte[] hash(Algorithm algorithm, byte[] input) {
        MessageDigest md = algorithm.getMessageDigest();
        md.update(input);
        return md.digest();
      }
    },
    HASH_FUNCTION_API() {
      @Override public byte[] hash(Algorithm algorithm, byte[] input) {
        return algorithm.getHashFunction().hashBytes(input).asBytes();
      }
    };
    public abstract byte[] hash(Algorithm algorithm, byte[] input);
  }

  private enum Algorithm {
    MD5("MD5", Hashing.md5()),
    SHA_1("SHA-1", Hashing.sha1()),
    SHA_256("SHA-256", Hashing.sha256()),
    SHA_384("SHA-384", Hashing.sha384()),
    SHA_512("SHA-512", Hashing.sha512());

    private final String algorithmName;
    private final HashFunction hashFn;
    Algorithm(String algorithmName, HashFunction hashFn) {
      this.algorithmName = algorithmName;
      this.hashFn = hashFn;
    }
    public MessageDigest getMessageDigest() {
      try {
        return MessageDigest.getInstance(algorithmName);
      } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
      }
    }
    public HashFunction getHashFunction() {
      return hashFn;
    }
  }

  // Use a constant seed for all of the benchmarks to ensure apples to apples comparisons.
  private static final int RANDOM_SEED = new Random().nextInt();

  private byte[] testBytes;

  @BeforeExperiment void setUp() {
    testBytes = new byte[size];
    new Random(RANDOM_SEED).nextBytes(testBytes);
  }

  @Benchmark byte hashing(int reps) {
    byte result = 0x01;
    HashMethod hashMethod = this.hashMethod;
    Algorithm algorithm = this.algorithm;
    for (int i = 0; i < reps; i++) {
      result ^= hashMethod.hash(algorithm, testBytes)[0];
    }
    return result;
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java MessageDigestAlgorithmBenchmark.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.