Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hash: Update hash.Hash methods #22001

Merged
merged 1 commit into from
Aug 7, 2024

Conversation

einar-hjortdal
Copy link
Contributor

@einar-hjortdal einar-hjortdal commented Aug 7, 2024

The hash.Hash interface was likely partially ported from Go.
Over time Vlang evolved and structs that implement the hash.Hash interface also gained new methods to bridge the differences between the Go version and the V version.
These new methods were not added to the hash.Hash interface.
This pull request adds these new methods to the hash.Hash interface.

@einar-hjortdal
Copy link
Contributor Author

In the future this interface may be improved a little bit:
I have not checked the source code of each implementation of sum, size, block_size method, I am not sure these are meant to have a mut receiver, however right now they are accepting a reference to the receiver, which should be about the same.

@einar-hjortdal einar-hjortdal changed the title Update hash.Hash methods hash: Update hash.Hash methods Aug 7, 2024
@spytheman
Copy link
Member

Why do you want to add the new methods? Do you have a specific example that would be enabled by them?
Keep in mind, that narrow interfaces are more widely useful than interfaces with lots of details.

@einar-hjortdal
Copy link
Contributor Author

einar-hjortdal commented Aug 7, 2024

The structs that implement the hash.Hash interface already implement these methods, as you can see by the implementation of md5, sha1, sha256 and sha512.
In Go, these methods aren't needed because Go does not abstract these operations: The write method for example isn't needed there, but in V it is needed. This difference has surfaced after the port of the interface.

Consider the following code:

	new_digest := fn [plugin_name] () hash.Hash {
		if plugin_name == 'Srp' {
			return sha1.new()
		}
		if plugin_name == 'Srp256' {
			return sha256.new()
		}
		panic('srp protocol error')
	}

	digest := new_digest()
	// TODO hash.Hash needs to have a write method.
	// hash.Hash is a port of Go's hash.Hash, but it differs significantly
	digest.write(n3.bytes())
	digest.write(n4.bytes())
	digest.write(salt)
	digest.write(key_a.bytes())
	digest.write(key_b.bytes())
	digest.write(key_k)
	key_m = digest.sum([]u8{})

In this situation, the function new_digest returns either a sha1.Digest or a sha256.Digest, these implement the hash.Hash interface. However, this Digest cannot use the .write method because the type hash.Hash does not have such method.

In Go, this is not an issue because hash.Hash embeds io.Writer

	if pluginName == "Srp" {
		digest = sha1.New()
	} else if pluginName == "Srp256" {
		digest = sha256.New()
	} else {
		panic("srp protocol error")
	}

	digest.Write(n3.Bytes())
	digest.Write(n4.Bytes())
	digest.Write(salt)
	digest.Write(keyA.Bytes())
	digest.Write(keyB.Bytes())
	digest.Write(keyK)
	keyM = digest.Sum(nil)

The change I propose bridges the difference and allows the hash.Hash interface to be used in a similar manner to Go's implementation

@spytheman spytheman merged commit 258aed4 into vlang:master Aug 7, 2024
61 checks passed
@einar-hjortdal
Copy link
Contributor Author

Note for future improvements:
For the .write method. we also have a io.Writer interface that could be embedded in hash.Hash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants