Java将散列转换为随机字符串

编程入门 行业动态 更新时间:2024-10-20 11:43:51
本文介绍了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将散列转换为随机字符串

本文发布于:2023-10-27 11:13:55,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   字符串   Java   将散列

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!