Are there in x86 any instructions to accelerate SHA (SHA1/2/256/512) encoding?

Intel has upcoming instructions for accelerating the calculation of SHA1 /256 hashes. You can read more about them, how to detect if your CPU support them and how to use them here. (But not SHA-512, you’ll still need to manually vectorize that with regular SIMD instructions. AVX512 should help for SHA-512 (and for SHA-1 / … Read more

Compute a hash from a stream of unknown length in C#

MD5, like other hash functions, does not require two passes. To start: HashAlgorithm hasher = ..; hasher.Initialize(); As each block of data arrives: byte[] buffer = ..; int bytesReceived = ..; hasher.TransformBlock(buffer, 0, bytesReceived, null, 0); To finish and retrieve the hash: hasher.TransformFinalBlock(new byte[0], 0, 0); byte[] hash = hasher.Hash; This pattern works for any … Read more

SecureRandom with NativePRNG vs SHA1PRNG

TL;DR: Use new SecureRandom() when you’re not sure and let the system figure it out. Possibly use SecureRandom.getInstanceStrong() for long term key generation. Do not expect a random number generator to generate a specific output sequence within a runtime application, not even if you seed it yourself. With random number generators it is always hard … Read more

Calculating HMACSHA256 using c# to match payment provider example

Edit: You likely are looking for a quick and simple way to do HMAC-SHA256 and not get into the finer details. The original question asks of those finer details which are explained further below. I want to perform a HMAC-SHA256 on a byte[] message input using System.Security.Cryptography; … private static byte[] HashHMAC(byte[] key, byte[] message) … Read more

How does a cryptographically secure random number generator work?

A cryptographically secure number random generator, as you might use for generating encryption keys, works by gathering entropy – that is, unpredictable input – from a source which other people can’t observe. For instance, /dev/random(4) on Linux collects information from the variation in timing of hardware interrupts from sources such as hard disks returning data, … Read more

MD5 hashing in Android

Here is an implementation you can use (updated to use more up to date Java conventions – for:each loop, StringBuilder instead of StringBuffer): public static String md5(final String s) { final String MD5 = “MD5”; try { // Create MD5 Hash MessageDigest digest = java.security.MessageDigest .getInstance(MD5); digest.update(s.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String … Read more

How to get truly random data, not random data fed into a PRNG seed like CSRNG’s do?

As you know, “truly random” means each of the bits is independent of everything else as well as uniformly distributed. However, this ideal is hard, if not impossible, to achieve in practice. In general, the closest way to get “truly random data” in practice is to gather hard-to-guess bits from nondeterministic sources, then condense those … Read more

iText/BouncyCastle ClassNotFound org.bouncycastle.asn1.DEREncodable and org.bouncycastle.tsp.TimeStampTokenInfo

iText marks bouncycastle dependencies as optional. If you require them, you need to add the dependencies in your own pom file. To find out which dependency to include in your project, open the itextpdf pom.xml file of the version you are using (for example 5.3.2, here) and search for the 2 bouncycastle dependencies. <dependency> <groupId>org.bouncycastle</groupId> … Read more