Skip to content

Add get_uid function for API resource

onyinyang requested to merge onyinyang/rdsys-backend-api:uid into main

This implements a get_uid function for resources in the API, however I wasn't able to get the UID produced by rust's crc64 crate to match go's. As I noted in the comment, Rust's crc64 crate makes use of "Jones" coefficients as outlined here: https://docs.rs/crc64/2.0.0/src/crc64/lib.rs.html#10 whereas the golang hash in rdsys uses a different polynomial. I tried changing the polynomial in the golang and testing the same functionality against the main.rs test that I included here. The golang equivalent(ish) is:

package main

import (
	"crypto/sha1"
	"encoding/hex"
	"fmt"
	"hash/crc64"
	"strings"
)

//const crc64Polynomial = 0x42F0E1EBA9EA3693

const crc64Polynomial = 0xAD93D23594C935A9

var crc64Table = crc64.MakeTable(crc64Polynomial)

type Hashkey uint64

func NewHashkey(id string) Hashkey {
	return Hashkey(crc64.Checksum([]byte(id), crc64Table))
}

func HashFingerprint(fingerprint string) (string, error) {

	fingerprint = strings.TrimSpace(fingerprint)

	rawFingerprint, err := hex.DecodeString(fingerprint)
	if err != nil {
		return "", err
	}

	rawHFingerprint := sha1.Sum(rawFingerprint)
	hFingerprint := hex.EncodeToString(rawHFingerprint[:])
	return strings.ToUpper(hFingerprint), nil
}

func main() {

	result, err := HashFingerprint("FD8DC7EF92F1F14D00CF9D6F6297A3468B59E707")
	if err != nil {
		fmt.Printf("Error!")
	}
	fmt.Printf("The result is: %s\n", result)
	hashed_result := NewHashkey("obfs4" + result)
	fmt.Printf("The result is: %v\n", hashed_result)
	hashed_result2 := NewHashkey("scramblesuit" + result)
	fmt.Printf("The result is: %v\n", hashed_result2)
}

I'm not sure if matching UIDs matter (not at this point anyway) between rdsys and the bridge distributor. The lox distributor would benefit from a UID that it can use internally to uniquely identify bridges coming from rdsys, but as long as there is consistency in how the hashing is done at the point of the API, I don't think it is especially important that it matches rdsys' UID. This could definitely create confusion later though which is the reason for the comment/documentation here.

Before merging main.rs should be removed.

Merge request reports