Skip to content

Commit 7002411

Browse files
committed
tools: add --sha3-256 support for checksumming the content that v download saves
1 parent dbd3e4b commit 7002411

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

cmd/tools/vdownload.v

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import flag
55
import net.http
66
import crypto.sha1
77
import crypto.sha256
8+
import crypto.sha3
89

910
struct Context {
1011
mut:
1112
show_help bool
1213
show_sha1 bool
1314
show_sha256 bool
15+
show_sha3_256 bool
1416
target_folder string
1517
output string
1618
continue_on_failure bool
@@ -38,9 +40,10 @@ fn main() {
3840
ctx.output = fp.string('output', `o`, '', 'Write output to the given file, instead of inferring it from the final part of the URL. All intermediate folders will be created, if they do not exist.')
3941
ctx.show_sha1 = fp.bool('sha1', `1`, false, 'Show the SHA1 hash of the downloaded file.')
4042
ctx.show_sha256 = fp.bool('sha256', `2`, false, 'Show the SHA256 hash of the downloaded file.')
43+
ctx.show_sha3_256 = fp.bool('sha3-256', `3`, false, 'Show the SHA3-256 (Keccak) hash of the downloaded file.')
4144
ctx.continue_on_failure = fp.bool('continue', `c`, false, 'Continue on download failures. If you download 5 URLs, and several of them fail, continue without error. False by default.')
42-
ctx.retries = fp.int('retries', `r`, 10, 'Number of retries, when an URL fails to download.')
43-
ctx.delay = time.Duration(u64(fp.float('delay', `d`, 1.0, 'Delay in seconds, after each retry.') * time.second))
45+
ctx.retries = fp.int('retries', `r`, 10, 'Number of retries, when an URL fails to download. The default is 10.')
46+
ctx.delay = time.Duration(u64(fp.float('delay', `d`, 1.0, 'Delay in seconds, after each retry. The default is 1 second.') * time.second))
4447
ctx.should_run = fp.bool('run', `R`, false, 'Run, after the script/program is completely downloaded.')
4548
ctx.delete_after_run = fp.bool('delete-after-run', `D`, false, 'Delete the downloaded script/program, after it has been run.')
4649
if ctx.show_help {
@@ -108,10 +111,9 @@ fn main() {
108111
log.info(' Removing: ${fpath}')
109112
os.rm(fpath) or {}
110113
}
111-
if !ctx.show_sha256 && !ctx.show_sha1 {
114+
if !ctx.show_sha256 && !ctx.show_sha1 && !ctx.show_sha3_256 {
112115
continue
113116
}
114-
115117
fbytes := os.read_bytes(fname)!
116118
if ctx.show_sha1 {
117119
mut digest1 := sha1.new()
@@ -120,14 +122,17 @@ fn main() {
120122
hash1 := sum1.hex()
121123
log.info(' SHA1: ${hash1}')
122124
}
123-
124125
if ctx.show_sha256 {
125126
mut digest256 := sha256.new()
126127
digest256.write(fbytes)!
127128
mut sum256 := digest256.sum([])
128129
hash256 := sum256.hex()
129130
log.info(' SHA256: ${hash256}')
130131
}
132+
if ctx.show_sha3_256 {
133+
hash3_256 := sha3.sum256(fbytes).hex()
134+
log.info(' SHA3-256: ${hash3_256}')
135+
}
131136
}
132137
println('Downloaded: ${downloaded} file(s) . Elapsed time: ${sw.elapsed()} . Errors: ${errors} .')
133138
if !ctx.continue_on_failure && errors > 0 {

vlib/v/help/other/download.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ Options:
1313
-o, --output <string> Write output to the given file, instead of inferring it from the final part of the URL. All intermediate folders will be created, if they do not exist.
1414
-1, --sha1 Show the SHA1 hash of the downloaded file.
1515
-2, --sha256 Show the SHA256 hash of the downloaded file.
16+
-3, --sha3-256 Show the SHA3-256 (Keccak) hash of the downloaded file.
1617
-c, --continue Continue on download failures. If you download 5 URLs, and several of them fail, continue without error. False by default.
17-
-r, --retries <int> Number of retries, when an URL fails to download.
18-
-d, --delay <float> Delay in seconds, after each retry.
18+
-r, --retries <int> Number of retries, when an URL fails to download. The default is 10.
19+
-d, --delay <float> Delay in seconds, after each retry. The default is 1 second.
1920
-R, --run Run, after the script/program is completely downloaded.
2021
-D, --delete-after-run Delete the downloaded script/program, after it has been run.

0 commit comments

Comments
 (0)