forked from vieter-v/vieter
				
			Working md5 checksum
							parent
							
								
									9bd14b4cbf
								
							
						
					
					
						commit
						df3310944e
					
				|  | @ -104,7 +104,7 @@ fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? { | ||||||
| 
 | 
 | ||||||
| fn main() { | fn main() { | ||||||
| 	// archive.list_filenames() | 	// archive.list_filenames() | ||||||
| 	res := pkg.read_pkg('test/homebank-5.5.3-1-x86_64.pkg.tar.zst') or { | 	res := pkg.read_pkg('test/jjr-joplin-desktop-2.6.10-4-x86_64.pkg.tar.zst') or { | ||||||
| 		eprintln(err.msg) | 		eprintln(err.msg) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
							
								
								
									
										16
									
								
								src/util.v
								
								
								
								
							
							
						
						
									
										16
									
								
								src/util.v
								
								
								
								
							|  | @ -2,7 +2,7 @@ module util | ||||||
| 
 | 
 | ||||||
| import os | import os | ||||||
| import crypto.md5 | import crypto.md5 | ||||||
| import crypto.sha256 | // import crypto.sha256 | ||||||
| 
 | 
 | ||||||
| // hash_file returns the md5 & sha256 hash of a given file | // hash_file returns the md5 & sha256 hash of a given file | ||||||
| // TODO actually implement sha256 | // TODO actually implement sha256 | ||||||
|  | @ -10,7 +10,7 @@ pub fn hash_file(path &string) ?(string, string) { | ||||||
| 	file := os.open(path) or { return error('Failed to open file.') } | 	file := os.open(path) or { return error('Failed to open file.') } | ||||||
| 
 | 
 | ||||||
| 	mut md5sum := md5.new() | 	mut md5sum := md5.new() | ||||||
| 	mut sha256sum := sha256.new() | 	// mut sha256sum := sha256.new() | ||||||
| 
 | 
 | ||||||
| 	buf_size := int(1_000_000) | 	buf_size := int(1_000_000) | ||||||
| 	mut buf := []byte{len: buf_size} | 	mut buf := []byte{len: buf_size} | ||||||
|  | @ -18,16 +18,16 @@ pub fn hash_file(path &string) ?(string, string) { | ||||||
| 
 | 
 | ||||||
| 	for bytes_left > 0 { | 	for bytes_left > 0 { | ||||||
| 		// TODO check if just breaking here is safe | 		// TODO check if just breaking here is safe | ||||||
| 		bytes_read := file.read(mut buf) or { break } | 		bytes_read := file.read(mut buf) or { return error('Failed to read from file.') } | ||||||
| 		bytes_left -= u64(bytes_read) | 		bytes_left -= u64(bytes_read) | ||||||
| 
 | 
 | ||||||
| 		if bytes_left > buf_size { | 		// For now we'll assume that this always works | ||||||
| 			// For now we'll assume that this always works | 		md5sum.write(buf[..bytes_read]) or { | ||||||
| 			md5sum.write(buf) or {} | 			return error('Failed to update checksum. This should never happen.') | ||||||
| 			// sha256sum.write(buf) or {} |  | ||||||
| 		} | 		} | ||||||
|  | 		// sha256sum.write(buf) or {} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// return md5sum.sum(buf).hex(), sha256sum.sum(buf).hex() | 	// return md5sum.sum(buf).hex(), sha256sum.sum(buf).hex() | ||||||
| 	return md5sum.sum(buf).hex(), '' | 	return md5sum.checksum().hex(), '' | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue