Java将散列转换为随机字符串 [英] Java convert hash to random string

查看:153
本文介绍了Java将散列转换为随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



缩减函数背后的基本原理是它需要一个哈希函数,执行一些计算,并返回一定长度的字符串。



目前,我正在使用SHA1散列函数,并且需要返回一个长度为三。我需要的字符串,由上任何三个随机字符从:

  abcdefghijklmnopqrstuvwxyz0123456789 

我面临的主要问题是我编写的任何简化函数总是返回已经生成的字符串。很好的还原功能很少会返回重复的字符串。



任何人都可以提出任何想法来完成这个任务吗?或者任何关于hash操作的建议都会很棒。



预先感谢

Josh

p>

解决方案

运用 KISS 原则:


  • SHA只是一个字符串

  • 字符串是足够随机的。
  • 整数 / li>


这行代码就是这样做的:

  public static String shortHash(String sha){
return Integer.toString(sha.hashCode()& 0x7FFFFFFF,36).substring(0,3);
}

注意:& 0x7FFFFFFF 是将符号位置零(散列代码可以是负数,否则会以前导减号呈现)。

编辑 - 保证哈希长度



我原来的解决方案很天真 - 它没有处理 int 哈希值的情况小于 100 (基数36) - 意味着它将打印少于3个字符。这段代码修正了这个问题,同时仍然保持随机的值。它还避免了 substring()调用,所以性能应该会更好。

  static int min = Integer.parseInt(100,36); 
static int range = Integer.parseInt(zzz,36) - min;

public static String shortHash(String sha){
return Integer.toString(min +(sha.hashCode()& 0x7FFFFFFF)%range,36);



$ b $ p
$ b

这段代码保证最终的散列有3个字符,强制它在 100 zzz - 基数36中最低和最高的3字符散列值,但仍然是随机。


I'm trying to develop a reduction function for use within a rainbow table generator.

The basic principle behind a reduction function is that it takes in a hash, performs some calculations, and returns a string of a certain length.

At the moment I'm using SHA1 hashes, and I need to return a string with a length of three. I need the string to be made up on any three random characters from:

abcdefghijklmnopqrstuvwxyz0123456789

The major problem I'm facing is that any reduction function I write, always returns strings that have already been generated. And a good reduction function will only return duplicate strings rarely.

Could anyone suggest any ideas on a way of accomplishing this? Or any suggestions at all on hash to string manipulation would be great.

Thanks in advance

Josh

解决方案

Applying the KISS principle:

  • An SHA is just a String
  • The JDK hashcode for String is "random enough"
  • Integer can render in any base

This single line of code does it:

public static String shortHash(String sha) {
    return Integer.toString(sha.hashCode() & 0x7FFFFFFF, 36).substring(0, 3);
}

Note: The & 0x7FFFFFFF is to zero the sign bit (hash codes can be negative numbers, which would otherwise render with a leading minus sign).

Edit - Guaranteeing hash length

My original solution was naive - it didn't deal with the case when the int hash is less than 100 (base 36) - meaning it would print less than 3 chars. This code fixes that, while still keeping the value "random". It also avoids the substring() call, so performance should be better.

static int min = Integer.parseInt("100", 36);
static int range = Integer.parseInt("zzz", 36) - min;

public static String shortHash(String sha) {
    return Integer.toString(min + (sha.hashCode() & 0x7FFFFFFF) % range, 36);
}

This code guarantees the final hash has 3 characters by forcing it to be between 100 and zzz - the lowest and highest 3-char hash in base 36, while still making it "random".

这篇关于Java将散列转换为随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆