Non-working hashing code [CI SKIP]

pull/48/head
Jef Roosens 2022-01-18 21:41:09 +01:00
parent 3dc355f786
commit cb16091f65
Signed by: Jef Roosens
GPG Key ID: 955C0660072F691F
3 changed files with 50 additions and 4 deletions

View File

@ -1,7 +1,19 @@
branches: [main, dev]
# branches: [main, dev]
platform: linux/amd64
pipeline:
dryrun:
image: woodpeckerci/plugin-docker-buildx
secrets: [ docker_username, docker_password ]
settings:
repo: chewingbever/vieter
tag: dev
platforms: [ linux/arm/v7, linux/arm64/v8, linux/amd64 ]
dry_run: true
when:
event: pull_request
branch: dev
dev:
image: woodpeckerci/plugin-docker-buildx
secrets: [ docker_username, docker_password ]

View File

@ -2,6 +2,7 @@ module pkg
import time
import os
import util
#flag -larchive
@ -120,8 +121,8 @@ fn parse_pkg_info_string(pkg_info_str &string) ?PkgInfo {
'arch' { pkg_info.arch = value }
'builddate' { pkg_info.build_date = value.int() }
'packager' { pkg_info.packager = value }
'md5sum' { pkg_info.md5sum = value }
'sha256sum' { pkg_info.sha256sum = value }
'md5sum' { continue }
'sha256sum' { continue }
'pgpsig' { pkg_info.pgpsig = value }
'pgpsigsize' { pkg_info.pgpsigsize = value.int() }
// Array values
@ -192,7 +193,11 @@ pub fn read_pkg(pkg_path string) ?Pkg {
}
mut pkg_info := parse_pkg_info_string(unsafe { cstring_to_vstring(&char(buf)) }) ?
pkg_info.csize = i64(os.file_size(pkg_path))
pkg_info.md5sum, pkg_info.sha256sum = util.hash_file(pkg_path) or {
return error('Failed to hash package.')
}
return Pkg{
info: pkg_info
@ -225,7 +230,6 @@ pub fn (p &PkgInfo) to_desc() string {
desc += format_entry('CSIZE', p.csize.str())
desc += format_entry('ISIZE', p.size.str())
// TODO calculate these
desc += format_entry('MD5SUM', p.md5sum)
desc += format_entry('SHA256SUM', p.sha256sum)

30
src/util.v 100644
View File

@ -0,0 +1,30 @@
module util
import os
import crypto.md5
import crypto.sha256
// hash_file returns the md5 & sha256 hash of a given file
pub fn hash_file(path &string) ?(string, string) {
file := os.open(path) or { return error('Failed to open file.') }
mut md5sum := md5.new()
mut sha256sum := sha256.new()
buf_size := i32(1_000_000)
mut buf := []byte{len: buf_size}
mut bytes_left := os.file_size(path)
for bytes_left > 0 {
// TODO check if just breaking here is safe
bytes_read := file.read(mut buf) or { break }
bytes_left -= u64(bytes_read)
if bytes_left > buf_size {
md5sum.write(buf)
sha256sum.write(buf)
}
}
return md5sum.sum(buf).hex(), sha256sum.sum(buf).hex()
}