필터 지우기
필터 지우기

how i can generate reverse of this below method(hash2string)-this method generate string2hash value...i have required an output- text as given in input side.

조회 수: 3 (최근 30일)
function hash=string2hash(str,type)
% This function generates a hash value from a text string
%
% hash=string2hash(str,type);
%
% inputs,
% str : The text string, or array with text strings.
% outputs,
% hash : The hash value, integer value between 0 and 2^32-1
% type : Type of has 'djb2' (default) or 'sdbm'
%
% From c-code on : http://www.cse.yorku.ca/~oz/hash.html
%
% djb2
% this algorithm was first reported by dan bernstein many years ago
% in comp.lang.c
%
% sdbm
% this algorithm was created for sdbm (a public-domain reimplementation of
% ndbm) database library. it was found to do well in scrambling bits,
% causing better distribution of the keys and fewer splits. it also happens
% to be a good general hashing function with good distribution.
%
% example,
%
% hash=string2hash('hello world');
% disp(hash);
%
% Function is written by D.Kroon University of Twente (June 2010)
% From string to double array
str=double(str);
if(nargin<2), type='djb2'; end
switch(type)
case 'djb2'
hash = 5381*ones(size(str,1),1);
for i=1:size(str,2),
hash = mod(hash * 33 + str(:,i), 2^32-1);
end
case 'sdbm'
hash = zeros(size(str,1),1);
for i=1:size(str,2),
hash = mod(hash * 65599 + str(:,i), 2^32-1);
end
otherwise
error('string_hash:inputs','unknown type');
end
  댓글 수: 1
SAURABH
SAURABH 2013년 6월 10일
편집: Walter Roberson 2013년 6월 10일
str=double(str);
if(nargin<2), type='djb2'; end
switch(type)
case 'djb2'
hash = 5381*ones(size(str,1),1);
for i=1:size(str,2),
hash = mod(hash * 33 + str(:,i), 2^32-1);
end
case 'sdbm'
hash = zeros(size(str,1),1);
for i=1:size(str,2),
hash = mod(hash * 65599 + str(:,i), 2^32-1);
end
otherwise
error('string_hash:inputs','unknown type');
end

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2013년 6월 10일
You cannot reverse that hash function. See http://dmytry.blogspot.ca/2009/11/horrible-hashes.html and notice
What is so stupid about it? For starters, even though the output of this function is 32 bits, not even for the 2 char ASCII strings do you have a guarantee for lack of collisions. In fact "SV" collides with "Pt", and "g5" collides with "as" [in the second version that uses xor], just to name two examples out of hundreds.
Therefor if your input string was even only as long as 2 characters, you might well not be able to reverse it because there would be other 2 character input strings that hashed to the same output string.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by