Skip to content

5. Randoms

nguyenvukhang edited this page Oct 21, 2024 · 1 revision

Why SHA?

The 7-character hashes used to uniquely identify marks in the project will be called SHAs. They used to be randomly generated but now we're just hashing the timestamp at which they are created, so they are rightfully hashes.

use std::hash::{Hash, Hasher, DefaultHasher};
use std::time::{SystemTime, UNIX_EPOCH};

const CHARSET: &[u8; 16] = b"abcdef0123456789";

fn random() -> [u8; 7] {
    let mut s = DefaultHasher::new();
    SystemTime::now().duration_since(UNIX_EPOCH).unwrap().hash(&mut s);
    let mut u: u64 = s.finish();
    std::array::from_fn(|_| CHARSET[((u % 16) as usize, u /= 16).0])
}

These are modelled after git's 40-character SHA-1 hashes to uniquely identify commits. "40-character SHA-1 hash" is a mouthful, so the community often call it something shorter in online discussions, often referring to it as a hash, or more specifically, the hash of a commit. Given enough time, some even started calling it a SHA.

  • GitHub documentation refers to it as a SHA (source).
  • GitHub Action's environment variable referring to this entity is named GITHUB_SHA (source).
  • StackOverflow discussions refer to them as SHA (s1, s2, s3).

I've considered calling it a Hash but I've decided against that because Hash is already a Rust trait. I've rejected the idea of Tag because the repository also deals with git tags, so there might be some confusion. Token technically works, but that brings in too many external ideas tied to authentication or even machine learning.

And so within this project, these will hereby be known as SHAs. Now if you see a line of Rust that goes

struct SHA([u8; 7]);

You'd know why.

 

Clone this wiki locally