github.com/zmap/zcrypto@v0.0.0-20240512203510-0fef58d9a9db/x509/fingerprint_test.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package x509
     6  
     7  import (
     8  	"encoding/json"
     9  	"testing"
    10  )
    11  
    12  var randomData = []byte("somerandomdata")
    13  
    14  type fpJSONTestStruct struct {
    15  	FP CertificateFingerprint `json:"fp"`
    16  }
    17  
    18  func TestMD5Fingerprint(t *testing.T) {
    19  	fingerprint := MD5Fingerprint(randomData)
    20  
    21  	if fingerprint.Hex() != "5698ed1e3d65a854fc702393fb2049b4" {
    22  		t.Fatal("invalid fingerprint:", fingerprint.Hex())
    23  	}
    24  	s := fpJSONTestStruct{
    25  		FP: fingerprint,
    26  	}
    27  	b, _ := json.Marshal(&s)
    28  	if `{"fp":"5698ed1e3d65a854fc702393fb2049b4"}` != string(b) {
    29  		t.Fatalf("invalid json: %s", b)
    30  	}
    31  }
    32  
    33  func TestSHA1Fingerprint(t *testing.T) {
    34  	fingerprint := SHA1Fingerprint(randomData)
    35  
    36  	if fingerprint.Hex() != "26f30f9a9ff52d1cfbd18c4ca4d54a898b05ce0d" {
    37  		t.Fatal("invalid fingerprint:", fingerprint.Hex())
    38  	}
    39  	s := fpJSONTestStruct{
    40  		FP: fingerprint,
    41  	}
    42  	b, _ := json.Marshal(&s)
    43  	if `{"fp":"26f30f9a9ff52d1cfbd18c4ca4d54a898b05ce0d"}` != string(b) {
    44  		t.Fatalf("invalid json: %s", b)
    45  	}
    46  }
    47  
    48  func TestSHA256Fingerprint(t *testing.T) {
    49  	fingerprint := SHA256Fingerprint(randomData)
    50  
    51  	if fingerprint.Hex() != "dbdffb426fe23336753b7ccc6ced25bafea6616c92e8922a3d857d95cf30d4f0" {
    52  		t.Fatal("invalid fingerprint:", fingerprint.Hex())
    53  	}
    54  	s := fpJSONTestStruct{
    55  		FP: fingerprint,
    56  	}
    57  	b, _ := json.Marshal(&s)
    58  	if `{"fp":"dbdffb426fe23336753b7ccc6ced25bafea6616c92e8922a3d857d95cf30d4f0"}` != string(b) {
    59  		t.Fatalf("invalid json: %s", b)
    60  	}
    61  }
    62  
    63  func TestSHA512Fingerprint(t *testing.T) {
    64  	fingerprint := SHA512Fingerprint(randomData)
    65  
    66  	if fingerprint.Hex() != "4e8a382161e2ee2fe460cbf99a2df371a7ce3b2587a637a6c3cec91fa2920ab969b40e4c9ec12ef12405e175d0b09baf35a46c4349e658def41b6d296bad3fd2" {
    67  		t.Fatal("invalid fingerprint:", fingerprint.Hex())
    68  	}
    69  	s := fpJSONTestStruct{
    70  		FP: fingerprint,
    71  	}
    72  	b, _ := json.Marshal(&s)
    73  	if `{"fp":"4e8a382161e2ee2fe460cbf99a2df371a7ce3b2587a637a6c3cec91fa2920ab969b40e4c9ec12ef12405e175d0b09baf35a46c4349e658def41b6d296bad3fd2"}` != string(b) {
    74  		t.Fatalf("invalid json: %s", b)
    75  	}
    76  }