com.absmartly.sdk.internal.hashing.Hashing Maven / Gradle / Ivy
package com.absmartly.sdk.internal.hashing;
import com.absmartly.sdk.internal.Buffers;
public abstract class Hashing {
private Hashing() {}
private static ThreadLocal threadBuffer = new ThreadLocal() {
@Override
public byte[] initialValue() {
return new byte[512];
}
};
public static byte[] hashUnit(CharSequence unit) {
final int n = unit.length();
final int bufferLen = n << 1;
byte[] buffer = threadBuffer.get();
if (buffer.length < bufferLen) {
final int bit = 32 - Integer.numberOfLeadingZeros(bufferLen - 1);
buffer = new byte[1 << bit];
threadBuffer.set(buffer);
}
final int encoded = Buffers.encodeUTF8(buffer, 0, unit);
return MD5.digestBase64UrlNoPadding(buffer, 0, encoded);
}
}