%for messages that are out of 128 ascii character alphabet, and only 1
%character as hash output
function hashing = netlimit(mes,ringlength,layernum)%L is number of different characters in alphabet,starlet is starting character of alphabet in decimal rep(ascii), ringlength is number of bits in each ring, numofhash is the number of desired hash numbers, layernum is min fix amount of layers
mes=double(mes);%decimal rep of each letter in message
s=7;% number of bits per letter
n=length(mes); %n=number of letters in message
N=s*n; %N=number of bits in message, for s bit rep of letters
lets=dec2bin(0:127,s);%to initialize set of all possibl letters in binary form
message=dec2bin(0,N);%to initialize message
hash=zeros(s,n+1);%to initialize matrix to be xored
hashlet=zeros(1,s);%to initialize vector represented final hash output
hashout=s;%number of bits of output hash
if rem(ringlength,s*log(2))>0;%if ringlength is not of correct size for model
    ringlength=(2^(floor(log(floor(ringlength/(s)))/log(2))+1))*s;
else
    ringlength=ringlength;
end;
unpadmes=(floor((N-1)/ringlength)+1);%number of layers needed to fit all bits of message
if unpadmes<=layernum;%if minimum layer amount is equal to or less than number of layers needed to fit all bits of message
    level=layernum;%actual amount of layers in petri-dish is minimum amount of layers
else%if minimum layer amount>number of layers needed to fit all bits of message
    level=unpadmes;%actual amount of layers in petri-dish =number of layers needed to fit all bits of message
end;
endmes=N-((unpadmes-1)*ringlength);%number of bits of message on last layer of layers needed to fit all bits of message
for p=1:n; %to determine what binary number each letter in the message is
   for e=1:128;
      for g=1:s;
          if ((mes(p)<e) && (mes(p)>e-2)); 
              message((s*p)-(s-g))=num2str(lets(e,g));
          end;
      end;
   end;
end;
if endmes<ringlength; %if message needs to be padded mid ring(of last layer of layers needed to fit all bits of message)
    for j=1:ringlength-endmes;%for each additional spot on the ring that needs padding
        fr=floor((j-1)/N);
        Pad1(j)=message(j-(fr*N));%repeat the bits of the message for each additional spot that the message does not fill
    end;
else %if message does not need to be padded mid ring
    Pad1='';%there is no padding 
end;
Padd='';
if unpadmes<level %if message needs to be padded for additional full layers
    for i=1:level-unpadmes;%for remaining levels that inital run of message does not fall on
        for j=1:ringlength;%for each spot on each layer (each spot on the ring)
            fr=floor((j-1)/N);
        Pad(i,j)=message(j-(fr*N));%repeat bits of message (or portion of bits of message) as many times as needed
        end;
        Padd=[Padd,Pad(i,:)];%make a row vector of all padding (mid ring and additional layers)
    end;
end;
message=[message,Pad1,Padd];%make a row vector of message and all padding (to fill all layers of petri-dish completely)
times=(log(ringlength/hashout))/(log(2));%number of times to xor from outside ring to inside ring
for g=1:level*ringlength; %to initiliaze first set of numbers to be xored
    hash(1,g)=str2num(message(g));
end;
if times>0; %if the circumference isn't equal to bits of hash output, xor inward until have amount of bits of hash output
    for j=2:times+1;%for each time the message must be xored inward
        for i=1:((ringlength*level)/(2^(j-1))); %for every other bit on the ring being xored 
            hash(j,i)=xor(hash((j-1),((2*i)-1)),hash((j-1),(2*i)));%xor that bit with the bit next to it
        end;
    end;
end;
hash=hash(times+1,:);%make matrix of bits of each layers hash values (each layer is a row)
for i=1:hashout;%for each bit of final hash output
    for j=1:level-1;%for each level (not counting first level)
        hash(i)=xor(hash(i),(hashout*j)+1);%xor each hash value with xored hash value on the layer above (just hash value on first level)
    end;
end;
for i=1:s;%for each bit of a character
    hashlet(i)=(hash(i))';%organize binary hash output in a row vector, with bits going accross row
end;
hashing=(bin2dec(num2str(hashlet)))';%convert each binary rep of hash character into its correspondng ascii decimal value