* Move parseHash16or32 into libutil, and use in nix-hash.
This commit is contained in:
		
							parent
							
								
									b12b21825c
								
							
						
					
					
						commit
						49f59dceca
					
				
					 5 changed files with 23 additions and 22 deletions
				
			
		| 
						 | 
				
			
			@ -204,6 +204,22 @@ Hash parseHash32(HashType ht, const string & s)
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Hash parseHash16or32(HashType ht, const string & s)
 | 
			
		||||
{
 | 
			
		||||
    Hash hash(ht);
 | 
			
		||||
    if (s.size() == hash.hashSize * 2)
 | 
			
		||||
        /* hexadecimal representation */
 | 
			
		||||
        hash = parseHash(ht, s);
 | 
			
		||||
    else if (s.size() == hashLength32(hash))
 | 
			
		||||
        /* base-32 representation */
 | 
			
		||||
        hash = parseHash32(ht, s);
 | 
			
		||||
    else
 | 
			
		||||
        throw Error(format("hash `%1%' has wrong length for hash type `%2%'")
 | 
			
		||||
            % s % printHashType(ht));
 | 
			
		||||
    return hash;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
bool isHash(const string & s)
 | 
			
		||||
{
 | 
			
		||||
    if (s.length() != 32) return false;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -58,6 +58,9 @@ string printHash32(const Hash & hash);
 | 
			
		|||
/* Parse a base-32 representation of a hash code. */
 | 
			
		||||
Hash parseHash32(HashType ht, const string & s);
 | 
			
		||||
 | 
			
		||||
/* Parse a base-16 or base-32 representation of a hash code. */
 | 
			
		||||
Hash parseHash16or32(HashType ht, const string & s);
 | 
			
		||||
 | 
			
		||||
/* Verify that the given string is a valid hash code. */
 | 
			
		||||
bool isHash(const string & s);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue