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

Incremental PR analysis: Implement the hashing #1372

Merged
merged 7 commits into from
Nov 22, 2022

Conversation

pavel-mikula-sonarsource
Copy link
Contributor

@pavel-mikula-sonarsource pavel-mikula-sonarsource commented Nov 17, 2022

Fixes #1369

It works with this Java counterpart:

  @Test
  public void fixme() throws Throwable {
    assertThat(contentHash("EmptyWithBom.cs")).isEqualTo("f1945cd6c19e56b3c1c78943ef5ec18116907a4ca1efc40a57d48ab1db7adfc5");
    assertThat(contentHash("EmptyNoBom.cs")).isEqualTo("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
    assertThat(contentHash("CodeWithBom.cs")).isEqualTo("b98aaf2ce5a3f9cdf8ab785563951f2309d577baa6351098f78908300fdc610a");
    assertThat(contentHash("CodeNoBom.cs")).isEqualTo("8c7535a8e3679bf8cc241b5749cef5fc38243401556f2b7869495c7b48ee4980");
    assertThat(contentHash("Utf8.cs")).isEqualTo("13aa54e315a806270810f3a91501f980a095a2ef1bcc53167d4c750a1b78684d");
    assertThat(contentHash("Utf16.cs")).isEqualTo("a9b3c4402770855d090ba4b49adeb5ad601cb3bbd6de18495302f45f242ef932");
    assertThat(contentHash("Ansi.cs")).isEqualTo("b965073262109da4f106cd90a5eeea025e2441c244af272537afa2cfb03c3ab8");
  }

  private String contentHash(String fileName) throws Throwable {
    MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
    byte[] content = Files.readAllBytes(Path.of("d:\\_Temp\\HashFiles\\", fileName));
    byte[] hash = sha256.digest(content);
    return bytesToHex(hash);
  }

  private static final byte[] HEX_ARRAY = "0123456789abcdef".getBytes(StandardCharsets.US_ASCII);

  private static String bytesToHex(byte[] bytes) {
    byte[] hexChars = new byte[bytes.length * 2];
    for (int i = 0; i < bytes.length; i++) {
      int v = bytes[i] & 0xFF; // FIXME: WTF?
      hexChars[i * 2] = HEX_ARRAY[v >>> 4];
      hexChars[i * 2 + 1] = HEX_ARRAY[v & 0x0F];
    }
    return new String(hexChars, StandardCharsets.UTF_8);
  }

The biggest pain is bytesToHex part 🤦‍♂️ 🤦‍♂️ 🤦‍♂️

// }
}

internal /* for testing */ string ContentHash(string path)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to use the async operations. Since here we target net46, we can use the FileStream class (using var stream = File.Open(path, FileMode.Open);).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have any other work to do meanwhile, so async will not help.

@sonarqubecloud
Copy link

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@pavel-mikula-sonarsource pavel-mikula-sonarsource merged commit 7faa560 into master Nov 22, 2022
@pavel-mikula-sonarsource pavel-mikula-sonarsource deleted the Pavel/Hash branch November 22, 2022 16:02
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.

Incremental PR analysis: Implement the hashing
2 participants