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

A git inspired blockchain filesystem #4

Open
frank-dspeed opened this issue Sep 2, 2022 · 0 comments
Open

A git inspired blockchain filesystem #4

frank-dspeed opened this issue Sep 2, 2022 · 0 comments

Comments

@frank-dspeed
Copy link
Member

frank-dspeed commented Sep 2, 2022

git is one of the best blockchain implementations that we got today

but its content hash is also based on the filename as you can see in refs/objects/<sha256-1 cont

const entrieHash = `${filename.length}\0${fs.readFileSync(filename)}`
file_name="your_file";
printf "blob $(wc -c < "$file_name")\0$(cat "$file_name")" | sha1sum

In Rust:

// Get the object ID
fn git_hash_object(file_content: &[u8]) -> Vec<u8> {

    let file_size = file_content.len().to_string();
    let hash_input: Vec<u8> = vec![
        "blob ".as_bytes(),
        file_size.as_bytes(),
        b"\0",
        file_content,
    ]
    // Flatten the Vec<&[u8]> to Vec<u8> with concat
    .concat();
    to_sha1(&hash_input)
}

fn to_sha1(hash_me: &[u8]) -> Vec<u8> {
    // using [crate sha1](https://docs.rs/sha1/0.6.0/sha1/) version 0.6.0 in to_sha1:
    let mut m = Sha1::new();
    m.update(hash_me);
    m.digest().bytes().to_vec()
}

git entry hash

bash version look if you got utf8 already

locale charmap

if it returns UTF-8

printf %s "filename" |  openssl dgst -sha256 –binary 

IFS read username from stdin as a sequence of bytes assumed to be encoded from characters as per the locale's encoding
if it is not utf-8 use iconv the utf 8 lib convert from locale encoding to UTF-8

IFS= read -r $filename
printf %s "$filename" |
  iconv -t utf-8 | 
  openssl dgst -sha256 –binary 
//printf "blob $(wc -c < "$file_name")\0$(cat "$file_name")" | sha1sum
 //[mode] [file name]\0[object ID]

tree object


The format of a tree object:

tree [content size]\0[Entries having references to other trees and blobs]
The format of each entry having references to other trees and blobs:

[mode] [file/folder name]\0[SHA-1 of referencing blob or tree]
I wrote a script deflating tree objects. It outputs as follows:

tree 192\0
40000 octopus-admin\0 a84943494657751ce187be401d6bf59ef7a2583c
40000 octopus-deployment\0 14f589a30cf4bd0ce2d7103aa7186abe0167427f
40000 octopus-product\0 ec559319a263bc7b476e5f01dd2578f255d734fd
100644 pom.xml\0 97e5b6b292d248869780d7b0c65834bfb645e32a
40000 src\0 6e63db37acba41266493ba8fb68c76f83f1bc9dd
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

No branches or pull requests

1 participant