├── LICENSE ├── README.md ├── README_CN.md ├── bencode ├── bencode.go ├── bencode_test.go ├── bytes.go ├── bytes_test.go ├── data.go ├── decode.go ├── empty_value.go ├── empty_value_test.go ├── encode.go ├── errors.go ├── errors_test.go ├── interface.go ├── misc.go ├── misc_test.go ├── multiple_torrent.go ├── scanner.go ├── scanner_test.go ├── single_torrent.go ├── tags.go └── tags_test.go ├── ber ├── asn1 │ ├── README.md │ ├── base128.go │ ├── ber_asn1_test.go │ ├── ber_test.go │ ├── bitstring.go │ ├── bool.go │ ├── common.go │ ├── decode.go │ ├── docs │ │ ├── encoding.png │ │ └── identifier-octet.png │ ├── encode.go │ ├── encode_test.go │ ├── errors.go │ ├── errors_test.go │ ├── integer.go │ ├── null.go │ ├── oid.go │ ├── options.go │ ├── real.go │ ├── string.go │ ├── tags.go │ ├── time.go │ └── uint.go └── ber.go ├── cipher ├── anubis │ ├── anubis.go │ ├── anubis_test.go │ ├── sbox.go │ └── utils.go ├── anubis2 │ ├── anubis.go │ ├── anubis_test.go │ ├── sbox.go │ └── utils.go ├── aria │ ├── aria.go │ ├── aria_test.go │ ├── sbox.go │ └── utils.go ├── ascon │ ├── ascon.go │ ├── ascon_test.go │ ├── state.go │ ├── testdata │ │ ├── vectors_128.txt │ │ └── vectors_128a.txt │ └── utils.go ├── belt │ ├── belt.go │ ├── belt_test.go │ ├── sboxs.go │ └── utils.go ├── camellia │ ├── camellia.go │ ├── camellia_test.go │ ├── sboxs.go │ └── utils.go ├── cascade │ ├── cascade.go │ └── cascade_test.go ├── cast │ ├── cast.go │ ├── cast_test.go │ ├── sboxs.go │ └── utils.go ├── cast256 │ ├── cast256.go │ ├── cast256_test.go │ ├── sboxs.go │ └── utils.go ├── clefia │ ├── clefia.go │ ├── clefia_test.go │ ├── sbox.go │ └── utils.go ├── crypton1 │ ├── crypton1.go │ ├── crypton1_test.go │ ├── sbox.go │ └── utils.go ├── curupira1 │ ├── curupira1.go │ ├── curupira1_test.go │ ├── letter_soup.go │ ├── marvin.go │ ├── sbox.go │ └── utils.go ├── des │ ├── des.go │ └── des_test.go ├── e2 │ ├── e2.go │ ├── e2_test.go │ ├── sbox.go │ └── utils.go ├── enigma │ ├── enigma.go │ └── enigma_test.go ├── fpe │ ├── ff1 │ │ ├── ff1.go │ │ └── ff1_test.go │ └── ff3 │ │ ├── ff3.go │ │ └── ff3_test.go ├── gost │ ├── gost.go │ ├── gost_test.go │ ├── sbox.go │ └── utils.go ├── grain │ ├── grain.go │ ├── grain_test.go │ ├── stream.go │ ├── testdata │ │ └── little_endian.txt │ └── utils.go ├── hc │ ├── hc.go │ ├── hc128.go │ ├── hc256.go │ ├── hc_test.go │ └── utils.go ├── hight │ ├── hight.go │ ├── hight_test.go │ └── sbox.go ├── idea │ ├── idea.go │ └── idea_test.go ├── kalyna │ ├── kalyna.go │ ├── kalyna128_128.go │ ├── kalyna128_256.go │ ├── kalyna256_256.go │ ├── kalyna256_512.go │ ├── kalyna512_512.go │ ├── kalyna_test.go │ ├── sbox.go │ └── utils.go ├── kasumi │ ├── kasumi.go │ ├── kasumi_test.go │ ├── sbox.go │ └── utils.go ├── kcipher2 │ ├── kcipher2.go │ ├── kcipher2_test.go │ ├── sbox.go │ └── utils.go ├── khazad │ ├── khazad.go │ ├── khazad_test.go │ └── sbox.go ├── kseed │ ├── kseed.go │ ├── kseed_test.go │ ├── sbox.go │ └── utils.go ├── kuznyechik │ ├── kuznyechik.go │ ├── kuznyechik_test.go │ ├── sbox.go │ └── utils.go ├── lea │ ├── lea.go │ ├── lea_test.go │ └── utils.go ├── lion │ ├── lion.go │ └── lion_test.go ├── loki97 │ ├── block.go │ ├── keygeneration.go │ ├── loki97.go │ ├── loki97_test.go │ ├── permutation.go │ ├── sboxes.go │ ├── ulong64.go │ └── ulong64_test.go ├── magenta │ ├── magenta.go │ ├── magenta_test.go │ ├── sbox.go │ └── utils.go ├── magma │ ├── magma.go │ └── magma_test.go ├── mars │ ├── mars.go │ ├── mars_test.go │ ├── sbox.go │ └── utils.go ├── mars2 │ ├── block.go │ ├── mars.go │ ├── mars_test.go │ └── utils.go ├── misty1 │ ├── misty1.go │ ├── misty1_test.go │ ├── sbox.go │ └── utils.go ├── multi2 │ ├── multi2.go │ ├── multi2_test.go │ └── utils.go ├── noekeon │ ├── noekeon.go │ ├── noekeon_test.go │ └── utils.go ├── panama │ ├── data.go │ ├── panama.go │ ├── panama_test.go │ ├── struct.go │ └── utils.go ├── present │ ├── present.go │ ├── present_test.go │ ├── sbox.go │ └── utils.go ├── rabbit │ ├── rabbit.go │ ├── rabbit_test.go │ ├── sbox.go │ └── utils.go ├── rc2 │ ├── rc2.go │ └── rc2_test.go ├── rc5 │ ├── cipher16.go │ ├── cipher32.go │ ├── cipher64.go │ ├── rc5.go │ └── rc5_test.go ├── rc6 │ ├── rc6.go │ └── rc6_test.go ├── rijndael │ ├── rijndael.go │ ├── rijndael_test.go │ ├── sbox.go │ └── utils.go ├── safer │ ├── safer.go │ ├── safer_test.go │ ├── sbox.go │ └── utils.go ├── saferplus │ ├── saferplus.go │ ├── saferplus_test.go │ ├── sbox.go │ └── utils.go ├── salsa20 │ ├── salsa20.go │ └── salsa20_test.go ├── seed │ ├── const.go │ ├── seed.go │ ├── seed128.go │ ├── seed256.go │ ├── seed_test.go │ └── utils.go ├── serpent │ ├── serpent.go │ ├── serpent_ref.go │ ├── serpent_test.go │ └── utils.go ├── shacal2 │ ├── sbox.go │ ├── shacal2.go │ ├── shacal2_test.go │ └── utils.go ├── simon │ ├── simon.go │ ├── simon128.go │ ├── simon192.go │ ├── simon256.go │ ├── simon_test.go │ └── utils.go ├── skein │ ├── sbox.go │ ├── skein.go │ ├── skein_test.go │ └── utils.go ├── skipjack │ ├── skipjack.go │ └── skipjack_test.go ├── sm4 │ ├── sbox.go │ ├── sm4.go │ ├── sm4_test.go │ └── utils.go ├── speck │ ├── speck.go │ ├── speck_test.go │ └── utils.go ├── spritz │ ├── hash.go │ ├── spritz.go │ └── spritz_test.go ├── square │ ├── square.go │ ├── square_test.go │ ├── tabs.go │ └── utils.go ├── threefish │ ├── threefish.go │ ├── threefish1024.go │ ├── threefish1024_test.go │ ├── threefish256.go │ ├── threefish256_test.go │ ├── threefish512.go │ ├── threefish512_test.go │ ├── threefish_test.go │ └── utils.go ├── threeway │ ├── threeway.go │ ├── threeway_test.go │ └── utils.go ├── trivium │ ├── trivium.go │ ├── trivium_test.go │ └── utils.go ├── twine │ ├── sbox.go │ ├── twine.go │ └── twine_test.go ├── wake │ ├── utils.go │ ├── wake.go │ └── wake_test.go └── xoodoo │ ├── xoodoo │ ├── example_test.go │ ├── xoodoo.go │ └── xoodoo_test.go │ └── xoodyak │ ├── aead.go │ ├── example_test.go │ ├── hash.go │ ├── mac.go │ ├── xoodyak.go │ └── xoodyak_test.go ├── cryptobin ├── bign │ ├── bign.go │ ├── bign_test.go │ ├── check.go │ ├── create.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── make.go │ ├── on.go │ ├── parse.go │ ├── sign.go │ ├── sign_test.go │ ├── to.go │ └── with.go ├── bip0340 │ ├── bip0340.go │ ├── bip0340_test.go │ ├── check.go │ ├── create.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── make.go │ ├── on.go │ ├── parse.go │ ├── sign.go │ ├── sign_test.go │ ├── to.go │ └── with.go ├── ca │ ├── alias.go │ ├── ca.go │ ├── ca_test.go │ ├── create.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── make.go │ ├── on.go │ ├── parse.go │ ├── to.go │ ├── verify.go │ └── with.go ├── crypto │ ├── config.go │ ├── cryptobin.go │ ├── cryptobin_test.go │ ├── data_set.go │ ├── encrypt.go │ ├── encrypt │ │ ├── aescfb │ │ │ ├── aescfb.go │ │ │ └── aescfb_test.go │ │ ├── aesecb │ │ │ ├── aesecb.go │ │ │ └── aesecb_test.go │ │ ├── anubis │ │ │ ├── anubis.go │ │ │ └── anubis_test.go │ │ ├── cast256 │ │ │ ├── cast256.go │ │ │ └── cast256_test.go │ │ ├── clefia │ │ │ ├── clefia.go │ │ │ └── clefia_test.go │ │ ├── crypton1 │ │ │ ├── crypton1.go │ │ │ └── crypton1_test.go │ │ ├── e2 │ │ │ ├── e2.go │ │ │ └── e2_test.go │ │ ├── enigma │ │ │ ├── enigma.go │ │ │ └── enigma_test.go │ │ ├── loki97 │ │ │ ├── loki97.go │ │ │ └── loki97_test.go │ │ ├── magenta │ │ │ ├── magenta.go │ │ │ └── magenta_test.go │ │ ├── mars │ │ │ ├── mars.go │ │ │ └── mars_test.go │ │ ├── mars2 │ │ │ ├── mars2.go │ │ │ └── mars2_test.go │ │ ├── noekeon │ │ │ ├── noekeon.go │ │ │ └── noekeon_test.go │ │ ├── panama │ │ │ ├── panama.go │ │ │ └── panama_test.go │ │ ├── salsa20 │ │ │ ├── salsa20.go │ │ │ └── salsa20_test.go │ │ ├── skipjack │ │ │ ├── skipjack.go │ │ │ └── skipjack_test.go │ │ ├── square │ │ │ ├── square.go │ │ │ └── square_test.go │ │ ├── threeway │ │ │ ├── threeway.go │ │ │ └── threeway_test.go │ │ └── wake │ │ │ ├── wake.go │ │ │ └── wake_test.go │ ├── encrypt_mode.go │ ├── encrypt_multiple.go │ ├── encrypt_padding.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── interface.go │ ├── mode │ │ ├── cfb16 │ │ │ ├── cfb16.go │ │ │ └── cfb16_test.go │ │ ├── cfb32 │ │ │ ├── cfb32.go │ │ │ └── cfb32_test.go │ │ ├── cfb64 │ │ │ ├── cfb64.go │ │ │ └── cfb64_test.go │ │ ├── eax │ │ │ ├── eax.go │ │ │ └── eax_test.go │ │ ├── g3413cbc │ │ │ ├── g3413cbc.go │ │ │ └── g3413cbc_test.go │ │ ├── g3413cfb │ │ │ ├── g3413cfb.go │ │ │ └── g3413cfb_test.go │ │ ├── g3413ctr │ │ │ ├── g3413ctr.go │ │ │ └── g3413ctr_test.go │ │ ├── g3413ofb │ │ │ ├── g3413ofb.go │ │ │ └── g3413ofb_test.go │ │ ├── gofb │ │ │ ├── gofb.go │ │ │ └── gofb_test.go │ │ ├── mgm │ │ │ ├── mgm.go │ │ │ └── mgm_test.go │ │ ├── ncfb │ │ │ ├── ncfb.go │ │ │ └── ncfb_test.go │ │ ├── nofb │ │ │ ├── nofb.go │ │ │ └── nofb_test.go │ │ ├── ocb │ │ │ ├── ocb.go │ │ │ └── ocb_test.go │ │ ├── ocb3 │ │ │ ├── ocb3.go │ │ │ └── ocb3_test.go │ │ └── wrap │ │ │ ├── wrap.go │ │ │ └── wrap_test.go │ ├── on.go │ ├── padding │ │ └── pkcs1 │ │ │ ├── pkcs1.go │ │ │ └── pkcs1_test.go │ ├── to.go │ ├── types.go │ ├── use.go │ └── with.go ├── dh │ ├── curve25519 │ │ ├── check.go │ │ ├── create.go │ │ ├── curve25519.go │ │ ├── curve25519_test.go │ │ ├── error.go │ │ ├── from.go │ │ ├── get.go │ │ ├── make.go │ │ ├── on.go │ │ ├── parse.go │ │ ├── to.go │ │ └── with.go │ ├── dh │ │ ├── check.go │ │ ├── create.go │ │ ├── dh.go │ │ ├── dh_test.go │ │ ├── error.go │ │ ├── from.go │ │ ├── get.go │ │ ├── make.go │ │ ├── on.go │ │ ├── parse.go │ │ ├── to.go │ │ ├── utils.go │ │ └── with.go │ └── ecdh │ │ ├── check.go │ │ ├── create.go │ │ ├── ecdh.go │ │ ├── ecdh_test.go │ │ ├── error.go │ │ ├── from.go │ │ ├── get.go │ │ ├── make.go │ │ ├── on.go │ │ ├── parse.go │ │ ├── to.go │ │ └── with.go ├── dsa │ ├── check.go │ ├── create.go │ ├── dsa.go │ ├── dsa_test.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── make.go │ ├── on.go │ ├── parse.go │ ├── sign.go │ ├── to.go │ └── with.go ├── ecdh │ ├── check.go │ ├── create.go │ ├── ecdh.go │ ├── ecdh_test.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── make.go │ ├── on.go │ ├── parse.go │ ├── to.go │ └── with.go ├── ecdsa │ ├── check.go │ ├── create.go │ ├── ecdsa.go │ ├── ecdsa_test.go │ ├── encryption.go │ ├── encryption_test.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── make.go │ ├── on.go │ ├── parse.go │ ├── sign.go │ ├── sign_test.go │ ├── to.go │ └── with.go ├── ecgdsa │ ├── check.go │ ├── create.go │ ├── ecgdsa.go │ ├── ecgdsa_test.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── make.go │ ├── on.go │ ├── parse.go │ ├── sign.go │ ├── sign_test.go │ ├── to.go │ └── with.go ├── ecsdsa │ ├── check.go │ ├── create.go │ ├── ecsdsa.go │ ├── ecsdsa_test.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── make.go │ ├── on.go │ ├── parse.go │ ├── sign.go │ ├── sign_test.go │ ├── to.go │ └── with.go ├── ed448 │ ├── check.go │ ├── create.go │ ├── doc.go │ ├── ed448.go │ ├── ed448_test.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── make.go │ ├── on.go │ ├── parse.go │ ├── sign.go │ ├── to.go │ └── with.go ├── eddsa │ ├── check.go │ ├── create.go │ ├── eddsa.go │ ├── eddsa_test.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── make.go │ ├── on.go │ ├── parse.go │ ├── sign.go │ ├── sign_test.go │ ├── to.go │ └── with.go ├── elgamal │ ├── check.go │ ├── create.go │ ├── elgamal.go │ ├── elgamal_test.go │ ├── encryption.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── make.go │ ├── on.go │ ├── parse.go │ ├── sign.go │ ├── to.go │ └── with.go ├── gost │ ├── check.go │ ├── create.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── gost.go │ ├── gost_test.go │ ├── make.go │ ├── on.go │ ├── parse.go │ ├── sign.go │ ├── to.go │ ├── vko.go │ └── with.go ├── rsa │ ├── check.go │ ├── create.go │ ├── encryption.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── make.go │ ├── on.go │ ├── parse.go │ ├── rsa.go │ ├── rsa_ecb_test.go │ ├── rsa_test.go │ ├── sign.go │ ├── sign_pss.go │ ├── to.go │ └── with.go ├── sm2 │ ├── check.go │ ├── create.go │ ├── encryption.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── make.go │ ├── on.go │ ├── parse.go │ ├── sign.go │ ├── sm2.go │ ├── sm2_test.go │ ├── to.go │ └── with.go └── ssh │ ├── check.go │ ├── create.go │ ├── error.go │ ├── from.go │ ├── get.go │ ├── make.go │ ├── on.go │ ├── parse.go │ ├── sign.go │ ├── sign_test.go │ ├── ssh.go │ ├── ssh_test.go │ ├── to.go │ └── with.go ├── ecdh ├── ecdh.go ├── ecdh_test.go ├── gmsm2.go ├── key.go ├── key │ ├── curves.go │ ├── pkcs8.go │ └── pkcs8_test.go ├── key_test.go ├── nist.go ├── test_curue │ ├── test_curue.go │ └── test_curue_test.go ├── x25519.go └── x448.go ├── elliptic ├── base_elliptic │ ├── curve.go │ ├── elliptic.go │ ├── int.go │ ├── int_test.go │ ├── test_add_b233_test.go │ ├── test_double_b233_test.go │ ├── test_test.go │ └── utils.go ├── bign │ ├── bign.go │ ├── bign_curves.go │ ├── bign_test.go │ ├── curve.go │ └── curve │ │ ├── fiat │ │ ├── Dockerfile │ │ ├── README │ │ ├── generate.go │ │ ├── p256.go │ │ ├── p256_fiat64.go │ │ ├── p256_invert.go │ │ ├── p384.go │ │ ├── p384_fiat64.go │ │ ├── p384_invert.go │ │ ├── p512.go │ │ ├── p512_fiat64.go │ │ └── p512_invert.go │ │ ├── generate.go │ │ ├── p256.go │ │ ├── p384.go │ │ └── p512.go ├── bitcurves │ ├── bitcurve.go │ ├── bitcurve_curves.go │ ├── bitcurve_test.go │ └── params.go ├── brainpool │ ├── brainpool.go │ ├── brainpool_test.go │ └── rcurve.go ├── curve256k1 │ ├── curve256k1.go │ ├── curve256k1_test.go │ ├── field │ │ ├── bench_test.go │ │ ├── fe.go │ │ └── fe_test.go │ ├── scalar.go │ ├── scalar_test.go │ ├── scalarmult.go │ ├── scalarmult_test.go │ ├── table.go │ └── table_test.go ├── edwards448 │ ├── edwards448.go │ ├── edwards448_test.go │ ├── field │ │ ├── bench_test.go │ │ ├── fe.go │ │ └── fe_test.go │ ├── scalar.go │ ├── scalar_test.go │ ├── scalarmult.go │ ├── scalarmult_test.go │ └── table.go ├── frp256v1 │ ├── frp256v1.go │ └── frp256v1_test.go ├── koblitz │ ├── koblitz.go │ └── koblitz_test.go ├── nist │ ├── nist.go │ ├── nist_curves.go │ ├── nist_test.go │ ├── test_oncurve_b233_test.go │ ├── test_oncurve_b283_test.go │ ├── test_oncurve_k233_test.go │ └── test_oncurve_k283_test.go ├── nums │ ├── nums.go │ ├── nums_curves.go │ ├── nums_test.go │ └── rcurve.go ├── secg │ ├── secg.go │ ├── secg_curves.go │ └── secg_test.go ├── secp │ ├── secp.go │ ├── secp_curves.go │ └── secp_test.go ├── secp256k1 │ ├── openssl_test.go │ ├── params.go │ ├── secp256k1.go │ └── secp256k1_test.go ├── tom │ ├── tom.go │ ├── tom_curves.go │ └── tom_test.go ├── weierstrass │ ├── weierstrass.go │ ├── weierstrass_curves.go │ └── weierstrass_test.go ├── wtls │ ├── wtls.go │ ├── wtls_curves.go │ └── wtls_test.go ├── x962 │ ├── x962.go │ ├── x962_curves.go │ └── x962_test.go └── x963 │ ├── x963.go │ ├── x963_curves.go │ └── x963_test.go ├── fernet ├── fernet.go ├── fernet_test.go ├── key.go ├── key_test.go └── utils.go ├── gm ├── sm2 │ ├── compress.go │ ├── key_exchange.go │ ├── key_pkcs1.go │ ├── key_pkcs1_test.go │ ├── key_pkcs8.go │ ├── key_pkcs8_test.go │ ├── keyexchange.go │ ├── keyexchange_test.go │ ├── marshal.go │ ├── sm2.go │ ├── sm2_test.go │ ├── sm2curve │ │ ├── field │ │ │ ├── field.go │ │ │ ├── field_fiat64.go │ │ │ ├── field_invert.go │ │ │ ├── field_order.go │ │ │ ├── field_order_test.go │ │ │ └── field_test.go │ │ ├── marshal.go │ │ ├── p256.go │ │ ├── sm2curve.go │ │ ├── sm2ec.go │ │ ├── sm2ec_asm.go │ │ ├── sm2ec_asm_amd64.s │ │ ├── sm2ec_asm_arm64.s │ │ ├── sm2ec_asm_ord.go │ │ ├── sm2ec_asm_table.bin │ │ ├── sm2ec_asm_table_test.go │ │ ├── sm2ec_asm_test.go │ │ ├── sm2ec_common_amd64.s │ │ ├── sm2ec_macros_amd64.s │ │ ├── sm2ec_mqv.go │ │ ├── sm2ec_mqv_test.go │ │ ├── sm2ec_ord.go │ │ ├── sm2ec_ord_test.go │ │ ├── sm2ec_plugin_amd64.s │ │ └── sm2ec_test.go │ └── utils.go ├── sm9 │ ├── encrypt.go │ ├── encrypt_cipher.go │ ├── encrypt_hash.go │ ├── encrypt_test.go │ ├── key_pkcs8.go │ ├── key_pkcs8_test.go │ ├── keyexchange.go │ ├── keyexchange_test.go │ ├── sign.go │ ├── sign_test.go │ ├── sm9curve │ │ ├── bn_pair.go │ │ ├── bn_pair_test.go │ │ ├── constants.go │ │ ├── curve.go │ │ ├── g1.go │ │ ├── g1_test.go │ │ ├── g2.go │ │ ├── g2_test.go │ │ ├── gfp.go │ │ ├── gfp12.go │ │ ├── gfp12_test.go │ │ ├── gfp2.go │ │ ├── gfp2_sqrt.go │ │ ├── gfp2_test.go │ │ ├── gfp6.go │ │ ├── gfp6_test.go │ │ ├── gfp_amd64.s │ │ ├── gfp_arm64.s │ │ ├── gfp_decl.go │ │ ├── gfp_generic.go │ │ ├── gfp_invert_sqrt.go │ │ ├── gfp_test.go │ │ ├── gt.go │ │ ├── gt_test.go │ │ ├── mul_amd64.h │ │ ├── mul_arm64.h │ │ ├── mul_bmi2_amd64.h │ │ ├── twist.go │ │ └── twist_test.go │ └── utils.go └── zuc │ ├── cipher.go │ ├── cipher_test.go │ ├── eea.go │ ├── eea_test.go │ ├── sbox.go │ ├── utils.go │ ├── zuc.go │ ├── zuc256.go │ ├── zuc256_mac.go │ ├── zuc256_test.go │ ├── zuc_mac.go │ └── zuc_test.go ├── go.mod ├── go.sum ├── hash ├── bash │ ├── bash.go │ ├── bash_test.go │ ├── digest.go │ ├── sbox.go │ └── utils.go ├── belt │ ├── belt.go │ ├── belt_test.go │ ├── digest.go │ ├── sbox.go │ └── utils.go ├── cmac │ ├── aes │ │ ├── cmac.go │ │ └── cmac_test.go │ ├── cmac.go │ ├── cmac_test.go │ ├── utils.go │ └── verify.go ├── gost │ ├── gost34112012 │ │ ├── hash.go │ │ └── hash_test.go │ ├── gost34112012256 │ │ ├── esptree.go │ │ ├── hash.go │ │ ├── hash_test.go │ │ ├── kdf.go │ │ └── tlstree.go │ ├── gost34112012512 │ │ ├── hash.go │ │ └── hash_test.go │ └── gost341194 │ │ ├── hash.go │ │ └── hash_test.go ├── has160 │ ├── digest.go │ ├── has160.go │ ├── has160_test.go │ ├── sbox.go │ └── utils.go ├── md2 │ ├── digest.go │ ├── md2.go │ ├── md2_test.go │ └── sbox.go ├── pmac │ ├── aes │ │ ├── pmac.go │ │ └── pmac_test.go │ ├── block.go │ ├── block_test.go │ ├── pmac.go │ ├── pmac_test.go │ ├── utils.go │ └── verify.go ├── rabin │ ├── chunker.go │ ├── chunker_test.go │ ├── poly.go │ ├── poly_test.go │ ├── rabin.go │ └── rabin_test.go └── sm3 │ ├── binary.go │ ├── binary_test.go │ ├── block.go │ ├── block_amd64.go │ ├── block_amd64.s │ ├── block_arm64.go │ ├── block_arm64.s │ ├── block_avx2_amd64.s │ ├── block_generic.go │ ├── block_ni_arm64.s │ ├── block_simd_amd64.s │ ├── const_asm.s │ ├── digest.go │ ├── gen_block_ni.go │ ├── rand.go │ ├── rand_df.go │ ├── rand_test.go │ ├── sbox.go │ ├── sm3.go │ ├── sm3_test.go │ └── utils.go ├── jceks ├── binary.go ├── bks.go ├── bks_decode.go ├── bks_encode.go ├── bks_entry.go ├── bks_test.go ├── bks_utils.go ├── cipher.go ├── cipher_blockcbc.go ├── cipher_derived_key.go ├── cipher_setting.go ├── consts.go ├── interfaces.go ├── jceks.go ├── jceks_decode.go ├── jceks_encode.go ├── jceks_entry.go ├── jceks_test.go ├── jceks_utils.go ├── jks.go ├── jks_decode.go ├── jks_encode.go ├── jks_encryptkey.go ├── jks_utils.go ├── pubkey.go ├── pubkey_dsa.go ├── pubkey_ecdsa.go ├── pubkey_eddsa.go ├── pubkey_rsa.go ├── pubkey_setting.go ├── pubkey_sm2.go ├── testdata │ ├── bks │ │ ├── christmas.bksv1 │ │ ├── christmas.bksv2 │ │ ├── custom_entry_passwords.bksv1 │ │ ├── custom_entry_passwords.bksv2 │ │ ├── empty.bksv1 │ │ └── empty.bksv2 │ ├── jceks │ │ ├── example-custom-oid.jceks │ │ ├── example-elliptic-sha1.jceks │ │ ├── example-root.jceks │ │ ├── example-sha1.jceks │ │ └── example-small-key.jceks │ ├── jks │ │ ├── DSA_1024_keystore.jks │ │ ├── EC_256_keystore.jks │ │ ├── RSA_2048_MD5withRSA_keystore.jks │ │ ├── RSA_2048_SHA1withRSA_keystore.jks │ │ ├── RSA_2048_SHA256withRSA_keystore.jks │ │ ├── RSA_2048_keystore.jks │ │ ├── RSA_2048_truststore.jks │ │ ├── androidstudio_default_123456_myalias.jks │ │ ├── diff_pass.jks │ │ └── windows_defaults_123456_mykey.jks │ └── uber │ │ ├── christmas.uber │ │ ├── custom_entry_passwords.uber │ │ └── empty.uber ├── uber.go ├── uber_decode.go └── uber_encode.go ├── kdf ├── argon2 │ ├── argon2.go │ ├── argon2_test.go │ ├── blake2b.go │ ├── blamka_amd64.go │ ├── blamka_amd64.s │ ├── blamka_generic.go │ └── blamka_ref.go ├── bcrypt_pbkdf │ ├── bcrypt_pbkdf.go │ └── bcrypt_pbkdf_test.go ├── gost_pbkdf2 │ ├── gost_pbkdf2.go │ └── gost_pbkdf2_test.go ├── gost_prfplus │ ├── gost_prfplus.go │ └── gost_prfplus_test.go ├── kbkdf │ ├── kbkdf.go │ ├── kbkdf_test.go │ └── prf.go ├── kdftree │ ├── kdftree.go │ └── kdftree_test.go ├── pbkdf │ ├── pbkdf.go │ └── pbkdf_test.go ├── pbkdf2 │ ├── pbkdf2.go │ ├── pbkdf2_test.go │ └── prf.go ├── pgp_s2k │ ├── iters.go │ ├── pgp_s2k.go │ ├── pgp_s2k_test.go │ └── s2k_family.go └── smkdf │ ├── smkdf.go │ ├── smkdf_64bit_test.go │ └── smkdf_test.go ├── logo.png ├── mac ├── ansi_retail_mac.go ├── cbc_mac.go ├── cbcr_mac.go ├── cmac.go ├── emac.go ├── lmac.go ├── mac.go ├── mac_des.go ├── mac_test.go └── tr_cbc_mac.go ├── mode ├── bc.go ├── bc_test.go ├── ccm │ ├── ccm.go │ └── ccm_test.go ├── cfb.go ├── cfb1.go ├── cfb16.go ├── cfb32.go ├── cfb64.go ├── cfb8.go ├── cfb_test.go ├── cfbs_test.go ├── eax │ ├── eax.go │ └── eax_test.go ├── ecb.go ├── ecb_test.go ├── g3413cbc.go ├── g3413cbc_test.go ├── g3413cfb.go ├── g3413cfb_test.go ├── g3413ctr.go ├── g3413ctr_test.go ├── g3413ofb.go ├── g3413ofb_test.go ├── gcfb.go ├── gcfb_test.go ├── gofb.go ├── gofb_test.go ├── hctr │ ├── hctr.go │ └── hctr_test.go ├── ige.go ├── ige_test.go ├── mgm │ ├── mgm.go │ └── mgm_test.go ├── ncfb.go ├── ncfb_test.go ├── nofb.go ├── nofb_test.go ├── ocb │ ├── ocb.go │ ├── ocb_test.go │ ├── random_vectors.go │ ├── rfc7253_test_vectors_suite_a.go │ └── rfc7253_test_vectors_suite_b.go ├── ocb3 │ ├── ocb3.go │ └── ocb3_test.go ├── ocfb.go ├── ofb.go ├── ofb8.go ├── ofb8_test.go ├── ofb_test.go ├── ofbnlf.go ├── ofbnlf_test.go ├── pcbc.go ├── siv │ ├── aes │ │ ├── siv.go │ │ └── siv_test.go │ ├── cipher.go │ ├── cipher_test.go │ ├── siv.go │ └── siv_test.go ├── wrap.go ├── wrap_pad.go └── wrap_test.go ├── padding ├── iso10126.go ├── iso7816_4.go ├── iso97971.go ├── padding.go ├── padding_test.go ├── pboc2.go ├── pkcs1.go ├── pkcs5.go ├── pkcs7.go ├── tbc.go ├── x923.go └── zero.go ├── passhash ├── argon2 │ ├── argon2.go │ └── argon2_test.go ├── argon2fmt │ ├── argon2fmt.go │ └── argon2fmt_test.go └── passhash9 │ ├── passhash9.go │ └── passhash9_test.go ├── pkcs ├── pbes1 │ ├── cipher.go │ ├── cipher_blockcbc.go │ ├── cipher_derived_key.go │ ├── cipher_rc4.go │ ├── cipher_setting.go │ ├── cipher_test.go │ └── helper.go ├── pbes2 │ ├── cipher.go │ ├── cipher_cbc.go │ ├── cipher_ccm.go │ ├── cipher_ccm_iv.go │ ├── cipher_cfb.go │ ├── cipher_cfb1.go │ ├── cipher_cfb8.go │ ├── cipher_ctr.go │ ├── cipher_ecb.go │ ├── cipher_gcm.go │ ├── cipher_gcm_iv.go │ ├── cipher_gost_cfb.go │ ├── cipher_ofb.go │ ├── cipher_rc2_cbc.go │ ├── cipher_rc5_cbc.go │ ├── cipher_setting.go │ ├── cipher_test.go │ └── helper.go ├── sym.go └── sym_test.go ├── pkcs1 ├── cipher.go ├── cipher_cbc.go ├── cipher_cfb.go ├── cipher_ctr.go ├── cipher_ofb.go ├── cipher_setting.go ├── helper.go ├── pkcs1.go └── pkcs1_test.go ├── pkcs12 ├── ber │ ├── pkcs12.go │ └── pkcs12_test.go ├── bmp_string.go ├── cipher.go ├── enveloped │ ├── cipher.go │ ├── enveloped.go │ ├── enveloped_test.go │ ├── interfaces.go │ ├── key_rsa.go │ ├── key_setting.go │ └── key_sm2.go ├── errors.go ├── key.go ├── key_dsa.go ├── key_ecdsa.go ├── key_eddsa.go ├── key_gost.go ├── key_rsa.go ├── key_setting.go ├── key_sm2.go ├── mac.go ├── mac_pbmac1.go ├── mac_test.go ├── p12.go ├── p12_cert.go ├── p12_crl.go ├── p12_decode.go ├── p12_encode.go ├── p12_interface.go ├── p12_option.go ├── p12_safebags.go ├── p12_test.go ├── p12_utils.go ├── pkcs12.go └── pkcs12_test.go ├── pkcs7 ├── cipher.go ├── decrypt.go ├── encrypt.go ├── hasher.go ├── hasher_setting.go ├── interfaces.go ├── key_rsa.go ├── key_setting.go ├── key_sm2.go ├── mode.go ├── pkcs7.go ├── pkcs7_test.go ├── sign.go ├── sign_enveloped.go ├── sign_enveloped_test.go ├── sign_test.go ├── signer.go ├── signer_dsa.go ├── signer_ecdsa.go ├── signer_eddsa.go ├── signer_rsa.go ├── signer_setting.go ├── signer_sm2.go ├── test_test.go ├── utils.go ├── verify.go └── verify_test.go ├── pkcs8 ├── pbes1 │ ├── cipher.go │ ├── pkcs8.go │ └── pkcs8_test.go ├── pbes2 │ ├── cipher.go │ ├── helper.go │ ├── kdf.go │ ├── kdf_pbkdf2.go │ ├── kdf_scrypt.go │ ├── kdf_smpbkdf2.go │ ├── pkcs8.go │ └── pkcs8_test.go ├── pkcs8.go └── pkcs8_test.go ├── pubkey ├── bign │ ├── bign.go │ ├── bign_test.go │ ├── curves.go │ ├── pkcs1.go │ ├── pkcs8.go │ ├── utils.go │ └── utils_test.go ├── bip0340 │ ├── batch.go │ ├── batch_test.go │ ├── bip0340.go │ ├── bip0340_test.go │ ├── curves.go │ ├── elliptic.go │ ├── key_test.go │ ├── params.go │ ├── pkcs1.go │ ├── pkcs8.go │ └── utils.go ├── dh │ ├── curve25519 │ │ ├── curve25519.go │ │ └── pkcs8.go │ ├── dh │ │ ├── dh.go │ │ ├── group.go │ │ └── pkcs8.go │ └── ecdh │ │ ├── ecdh.go │ │ └── pkcs8.go ├── dsa │ ├── dsa_test.go │ ├── pkcs1.go │ ├── pkcs8.go │ ├── xml.go │ └── xml_test.go ├── ecdsa │ ├── curves.go │ ├── decdsa.go │ ├── decdsa_test.go │ ├── ecdsa_test.go │ ├── pkcs1.go │ └── pkcs8.go ├── ecfsdsa │ ├── ecfsdsa.go │ └── ecfsdsa_test.go ├── ecgdsa │ ├── curves.go │ ├── ecgdsa.go │ ├── ecgdsa_test.go │ ├── pkcs1.go │ └── pkcs8.go ├── ecies │ ├── ecies.go │ └── ecies_test.go ├── eckcdsa │ ├── curves.go │ ├── eckcdsa.go │ ├── eckcdsa_test.go │ ├── pkcs1.go │ └── pkcs8.go ├── ecosdsa │ ├── ecosdsa.go │ └── ecosdsa_test.go ├── ecrdsa │ ├── ecrdsa.go │ └── ecrdsa_test.go ├── ecsdsa │ ├── curves.go │ ├── ecsdsa.go │ ├── ecsdsa_test.go │ ├── key_test.go │ ├── pkcs1.go │ └── pkcs8.go ├── ed448 │ ├── ed448.go │ ├── ed448_test.go │ ├── pkcs8.go │ └── pkcs8_test.go ├── egdsa │ ├── egdsa.go │ ├── egdsa_test.go │ └── xml.go ├── elgamal │ ├── elgamal.go │ ├── elgamal_test.go │ ├── pkcs1.go │ ├── pkcs8.go │ └── xml.go ├── gost │ ├── curve.go │ ├── curves.go │ ├── edwards.go │ ├── gost.go │ ├── gost_test.go │ ├── marshal.go │ ├── params.go │ ├── pkcs8.go │ ├── pkcs8_test.go │ ├── utils.go │ ├── vko.go │ └── vko_test.go ├── kcdsa │ ├── kcdsa.go │ ├── kcdsa_test.go │ ├── key.go │ ├── parameter.go │ ├── ppgf.go │ ├── ppgf_test.go │ ├── testcases_2048_224_SHA224_test.go │ ├── testcases_2048_256_SHA256_test.go │ ├── testcases_ttak_test.go │ └── utils.go ├── lms │ ├── consts.go │ ├── hss.go │ ├── hss_test.go │ ├── lmots.go │ ├── lmots_params.go │ ├── lmots_test.go │ ├── lms.go │ ├── lms_params.go │ ├── lms_test.go │ ├── params.go │ └── utils.go ├── rabin │ ├── rabin.go │ ├── rabin_test.go │ └── utils.go ├── rsa │ ├── encryption.go │ ├── encryption_test.go │ ├── lower_safe.go │ ├── lower_safe_test.go │ ├── xml.go │ └── xml_test.go ├── sdsa │ ├── sdsa.go │ ├── sdsa_test.go │ ├── xml.go │ └── xml_test.go ├── x25519 │ ├── fuzz_go1.20_test.go │ ├── x25519.go │ ├── x25519_go1.20.go │ ├── x25519_go1.20_test.go │ ├── x25519_not_go1.20.go │ └── x25519_test.go ├── x448 │ ├── x448.go │ └── x448_test.go └── xmss │ ├── hash.go │ ├── hash_address.go │ ├── params.go │ ├── sm3xmss │ ├── params.go │ ├── xmss.go │ └── xmss_test.go │ ├── tree.go │ ├── utils.go │ ├── wots.go │ ├── wots_test.go │ ├── xmss.go │ ├── xmss │ ├── params.go │ ├── xmss.go │ └── xmss_test.go │ ├── xmss_test.go │ └── xmssmt │ ├── params.go │ ├── xmssmt.go │ └── xmssmt_test.go ├── rand ├── drbg │ ├── ctr.go │ ├── ctr_test.go │ ├── hash.go │ ├── hash_test.go │ ├── hmac.go │ ├── hmac_test.go │ └── utils.go ├── keygen │ ├── keygen.go │ └── keygen_test.go ├── prng │ ├── prng.go │ └── prng_test.go ├── randomart │ ├── randomart.go │ └── randomart_test.go └── zero │ ├── zero.go │ └── zero_test.go ├── ssh ├── cipher.go ├── cipher_cbc.go ├── cipher_chacha20poly1305.go ├── cipher_ctr.go ├── cipher_gcm.go ├── cipher_rc4.go ├── cipher_setting.go ├── helper.go ├── kdf.go ├── kdf_bcrypt.go ├── kdf_bcryptbin.go ├── key.go ├── key_dsa.go ├── key_ecdsa.go ├── key_eddsa.go ├── key_rsa.go ├── key_setting.go ├── key_sm2.go ├── keys.go ├── keys_sm2.go ├── ssh.go ├── ssh_test.go └── ssh_utils.go ├── tool ├── alias │ ├── alias.go │ └── alias_test.go ├── binary │ ├── binary.go │ └── binary_test.go ├── bmp_string │ └── bmp_string.go ├── bytes │ ├── bytes.go │ └── bytes_test.go ├── cipher │ ├── cipher.go │ └── cipher_test.go ├── config │ └── config.go ├── constraints │ └── constraints.go ├── encoding │ ├── encoding.go │ └── encoding_test.go ├── error_event │ └── error_event.go ├── errors │ ├── errors.go │ └── join.go ├── expect │ └── assert.go ├── hash │ ├── crypto.go │ ├── crypto_test.go │ ├── hash.go │ └── hash_test.go ├── math │ ├── gcd │ │ ├── extended.go │ │ ├── extended_test.go │ │ ├── extendedgcd.go │ │ ├── extendedgcd_test.go │ │ ├── extendedgcditerative.go │ │ ├── gcd.go │ │ ├── gcd_test.go │ │ └── gcditerative.go │ ├── lcm │ │ ├── lcm.go │ │ └── lcm_test.go │ ├── math.go │ └── math_test.go ├── memory │ ├── aligned.go │ ├── convert.go │ ├── memclr_asm.go │ ├── memclr_generic.go │ ├── memclr_test.go │ ├── memset.go │ └── ptr.go ├── pem │ └── pem.go ├── recover │ ├── recover.go │ └── recover_test.go ├── test │ ├── assert.go │ ├── assert_test.go │ ├── format.go │ ├── format_test.go │ ├── test.go │ └── test_test.go ├── utils │ ├── utils.go │ └── utils_test.go └── xor │ ├── xor_amd64.go │ ├── xor_amd64.s │ ├── xor_arm64.go │ ├── xor_arm64.s │ └── xor_generic.go └── x509 ├── boring.go ├── boring_test.go ├── cert_pool.go ├── cert_pool_test.go ├── csr_enveloped.go ├── csr_enveloped_test.go ├── csr_response.go ├── csr_response_test.go ├── fipstls ├── stub.s └── tls.go ├── hash.go ├── hash_test.go ├── notboring.go ├── parser.go ├── pkcs8.go ├── pkcs8_test.go ├── util.go ├── verify.go ├── verify_test.go ├── x509.go └── x509_test.go /bencode/bytes.go: -------------------------------------------------------------------------------- 1 | package bencode 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | ) 7 | 8 | // bytes data 9 | type Bytes []byte 10 | 11 | var ( 12 | _ Unmarshaler = (*Bytes)(nil) 13 | _ Marshaler = (*Bytes)(nil) 14 | _ Marshaler = Bytes{} 15 | ) 16 | 17 | // Unmarshal Bytes 18 | func (me *Bytes) UnmarshalBencode(b []byte) error { 19 | *me = append([]byte(nil), b...) 20 | return nil 21 | } 22 | 23 | // Marshal Bytes 24 | func (me Bytes) MarshalBencode() ([]byte, error) { 25 | if len(me) == 0 { 26 | return nil, errors.New("marshalled Bytes should not be zero-length") 27 | } 28 | 29 | return me, nil 30 | } 31 | 32 | // output string 33 | func (me Bytes) GoString() string { 34 | return fmt.Sprintf("bencode.Bytes(%q)", []byte(me)) 35 | } 36 | -------------------------------------------------------------------------------- /bencode/bytes_test.go: -------------------------------------------------------------------------------- 1 | package bencode 2 | 3 | import ( 4 | "testing" 5 | 6 | cryptobin_test "github.com/deatil/go-cryptobin/tool/test" 7 | ) 8 | 9 | func Test_Bytes(t *testing.T) { 10 | assertEqual := cryptobin_test.AssertEqualT(t) 11 | assertNoError := cryptobin_test.AssertNoErrorT(t) 12 | 13 | data := []byte("123123") 14 | 15 | var buf Bytes 16 | err := buf.UnmarshalBencode(data) 17 | 18 | assertNoError(err, "Bytes-UnmarshalBencode") 19 | 20 | res, err := buf.MarshalBencode() 21 | 22 | assertNoError(err, "Bytes-MarshalBencode") 23 | assertEqual(res, data, "Bytes-MarshalBencode") 24 | 25 | res2 := buf.GoString() 26 | 27 | assertEqual(res2, "bencode.Bytes(\"123123\")", "Bytes-GoString") 28 | } 29 | -------------------------------------------------------------------------------- /bencode/empty_value_test.go: -------------------------------------------------------------------------------- 1 | package bencode 2 | 3 | import ( 4 | "reflect" 5 | "testing" 6 | 7 | cryptobin_test "github.com/deatil/go-cryptobin/tool/test" 8 | ) 9 | 10 | func Test_IsEmptyValue(t *testing.T) { 11 | eq := cryptobin_test.AssertEqualT(t) 12 | 13 | type S struct { 14 | F string `bencode:"go-cryptobin,ddd" color:"blue,red" lang` 15 | } 16 | 17 | var tmap map[string]string 18 | var tslice []string 19 | var tarray []byte 20 | var tStruct S 21 | var tfunc = func(){} 22 | 23 | tfunc = nil 24 | 25 | eq(IsEmptyValue(reflect.ValueOf(tmap)), true, "Test_IsEmptyValue-1") 26 | eq(IsEmptyValue(reflect.ValueOf(tslice)), true, "Test_IsEmptyValue-2") 27 | eq(IsEmptyValue(reflect.ValueOf(tarray)), true, "Test_IsEmptyValue-3") 28 | eq(IsEmptyValue(reflect.ValueOf(tStruct)), true, "Test_IsEmptyValue-31") 29 | eq(IsEmptyValue(reflect.ValueOf(tfunc)), true, "Test_IsEmptyValue-5") 30 | eq(IsEmptyValue(reflect.ValueOf(0)), true, "Test_IsEmptyValue-6") 31 | } 32 | -------------------------------------------------------------------------------- /bencode/interface.go: -------------------------------------------------------------------------------- 1 | package bencode 2 | 3 | // Any type which implements this interface, will be marshaled using the 4 | // specified method. 5 | type Marshaler interface { 6 | MarshalBencode() ([]byte, error) 7 | } 8 | 9 | // Any type which implements this interface, will be unmarshaled using the 10 | // specified method. 11 | type Unmarshaler interface { 12 | UnmarshalBencode([]byte) error 13 | } 14 | -------------------------------------------------------------------------------- /bencode/misc.go: -------------------------------------------------------------------------------- 1 | package bencode 2 | 3 | import ( 4 | "fmt" 5 | "unsafe" 6 | "reflect" 7 | ) 8 | 9 | // Wow Go is retarded. 10 | var ( 11 | marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem() 12 | unmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem() 13 | ) 14 | 15 | func bytesAsString(b []byte) string { 16 | return *(*string)(unsafe.Pointer(&b)) 17 | } 18 | 19 | // splitPieceHashes 切割Pieces 20 | func splitPieceHashes(pieces string) ([][20]byte, error) { 21 | // SHA-1 hash的长度 22 | hashLen := 20 23 | buf := []byte(pieces) 24 | 25 | if len(buf)%hashLen != 0 { 26 | // 片段的长度不正确 27 | err := fmt.Errorf("Received malformed pieces of length %d", len(buf)) 28 | return nil, err 29 | } 30 | 31 | // hash 的总数 32 | numHashes := len(buf) / hashLen 33 | hashes := make([][20]byte, numHashes) 34 | 35 | for i := 0; i < numHashes; i++ { 36 | copy(hashes[i][:], buf[i*hashLen:(i+1)*hashLen]) 37 | } 38 | 39 | return hashes, nil 40 | } 41 | -------------------------------------------------------------------------------- /bencode/misc_test.go: -------------------------------------------------------------------------------- 1 | package bencode 2 | 3 | import ( 4 | "testing" 5 | 6 | cryptobin_test "github.com/deatil/go-cryptobin/tool/test" 7 | ) 8 | 9 | func Test_bytesAsString(t *testing.T) { 10 | eq := cryptobin_test.AssertEqualT(t) 11 | 12 | d := []byte("test-data") 13 | 14 | eq(bytesAsString(d), "test-data", "Test_bytesAsString") 15 | } 16 | 17 | func Test_splitPieceHashes(t *testing.T) { 18 | eq := cryptobin_test.AssertEqualT(t) 19 | noErr := cryptobin_test.AssertNoErrorT(t) 20 | 21 | d := "test--data1234567890data..test0987654321" 22 | res, err := splitPieceHashes(d) 23 | noErr(err, "Test_splitPieceHashes") 24 | 25 | check := [][20]byte{ 26 | [20]byte([]byte("test--data1234567890")), 27 | [20]byte([]byte("data..test0987654321")), 28 | } 29 | eq(res, check, "Test_bytesAsString") 30 | } 31 | -------------------------------------------------------------------------------- /bencode/scanner.go: -------------------------------------------------------------------------------- 1 | package bencode 2 | 3 | import ( 4 | "io" 5 | "errors" 6 | ) 7 | 8 | // Implements io.ByteScanner over io.Reader, for use in Decoder, to ensure 9 | // that as little as the undecoded input Reader is consumed as possible. 10 | type scanner struct { 11 | r io.Reader 12 | b [1]byte // Buffer for ReadByte 13 | unread bool // True if b has been unread, and so should be returned next 14 | } 15 | 16 | func (me *scanner) Read(b []byte) (int, error) { 17 | return me.r.Read(b) 18 | } 19 | 20 | func (me *scanner) ReadByte() (byte, error) { 21 | if me.unread { 22 | me.unread = false 23 | return me.b[0], nil 24 | } 25 | 26 | n, err := me.r.Read(me.b[:]) 27 | if n == 1 { 28 | err = nil 29 | } 30 | 31 | return me.b[0], err 32 | } 33 | 34 | func (me *scanner) UnreadByte() error { 35 | if me.unread { 36 | return errors.New("byte already unread") 37 | } 38 | 39 | me.unread = true 40 | 41 | return nil 42 | } 43 | -------------------------------------------------------------------------------- /bencode/tags.go: -------------------------------------------------------------------------------- 1 | package bencode 2 | 3 | import ( 4 | "reflect" 5 | "strings" 6 | ) 7 | 8 | func getTag(st reflect.StructTag) tag { 9 | return parseTag(st.Get("bencode")) 10 | } 11 | 12 | type tag []string 13 | 14 | func parseTag(tagStr string) tag { 15 | return strings.Split(tagStr, ",") 16 | } 17 | 18 | func (me tag) Ignore() bool { 19 | return me[0] == "-" 20 | } 21 | 22 | func (me tag) Key() string { 23 | return me[0] 24 | } 25 | 26 | func (me tag) HasOpt(opt string) bool { 27 | if len(me) < 1 { 28 | return false 29 | } 30 | for _, s := range me[1:] { 31 | if s == opt { 32 | return true 33 | } 34 | } 35 | return false 36 | } 37 | 38 | func (me tag) OmitEmpty() bool { 39 | return me.HasOpt("omitempty") 40 | } 41 | 42 | func (me tag) IgnoreUnmarshalTypeError() bool { 43 | return me.HasOpt("ignore_unmarshal_type_error") 44 | } 45 | -------------------------------------------------------------------------------- /ber/asn1/base128.go: -------------------------------------------------------------------------------- 1 | package asn1 2 | 3 | import "bytes" 4 | 5 | // https://en.wikipedia.org/wiki/Variable-length_quantity 6 | 7 | func reverseBytes(b []byte) []byte { 8 | for i, j := 0, len(b)-1; i < len(b)/2; i++ { 9 | b[i], b[j-i] = b[j-i], b[i] 10 | } 11 | 12 | return b 13 | } 14 | 15 | func encodeBase128(n int) []byte { 16 | buf := new(bytes.Buffer) 17 | 18 | for n != 0 { 19 | i := n & 0x7f // b01111111 20 | n >>= 7 21 | 22 | if len(buf.Bytes()) != 0 { 23 | i |= 0x80 // b10000000 24 | } 25 | buf.WriteByte(byte(i)) 26 | } 27 | 28 | return reverseBytes(buf.Bytes()) 29 | } 30 | -------------------------------------------------------------------------------- /ber/asn1/bool.go: -------------------------------------------------------------------------------- 1 | package asn1 2 | 3 | // FLAG 4 | 5 | // A Flag accepts any data and is set to true if present. 6 | type Flag bool 7 | 8 | type boolEncoder bool 9 | 10 | func NewBoolEncoder(d bool) boolEncoder { 11 | i := boolEncoder(d) 12 | 13 | return i 14 | } 15 | 16 | func NewBoolEncoderWithInt(d int) boolEncoder { 17 | i := boolEncoder(false) 18 | 19 | if d != 0 { 20 | i = boolEncoder(true) 21 | } 22 | 23 | return i 24 | } 25 | 26 | func (b boolEncoder) length() int { 27 | return 1 28 | } 29 | 30 | func (e boolEncoder) encode() ([]byte, error) { 31 | if e { 32 | return []byte{0xff}, nil 33 | } 34 | 35 | return []byte{0x00}, nil 36 | } 37 | -------------------------------------------------------------------------------- /ber/asn1/docs/encoding.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/ber/asn1/docs/encoding.png -------------------------------------------------------------------------------- /ber/asn1/docs/identifier-octet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/ber/asn1/docs/identifier-octet.png -------------------------------------------------------------------------------- /ber/asn1/errors.go: -------------------------------------------------------------------------------- 1 | package asn1 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | ) 7 | 8 | type UnsupportedTypeError struct { 9 | Type reflect.Type 10 | } 11 | 12 | func (e *UnsupportedTypeError) Error() string { 13 | return fmt.Sprintf("asn1: unsupported type: %s", e.Type.String()) 14 | } 15 | 16 | // A SyntaxError suggests that the ASN.1 data is invalid. 17 | type SyntaxError struct { 18 | Msg string 19 | } 20 | 21 | func (e SyntaxError) Error() string { 22 | return "asn1: syntax error: " + e.Msg 23 | } 24 | 25 | // A StructuralError suggests that the ASN.1 data is valid, but the Go type 26 | // which is receiving it doesn't match. 27 | type StructuralError struct { 28 | Msg string 29 | } 30 | 31 | func (e StructuralError) Error() string { 32 | return "asn1: structure error: " + e.Msg 33 | } 34 | -------------------------------------------------------------------------------- /ber/asn1/errors_test.go: -------------------------------------------------------------------------------- 1 | package asn1 2 | 3 | import ( 4 | "reflect" 5 | "testing" 6 | 7 | cryptobin_test "github.com/deatil/go-cryptobin/tool/test" 8 | ) 9 | 10 | func Test_UnsupportedTypeError(t *testing.T) { 11 | assertEqual := cryptobin_test.AssertEqualT(t) 12 | 13 | mte := &UnsupportedTypeError{ 14 | Type: reflect.TypeOf("test"), 15 | } 16 | 17 | check := "asn1: unsupported type: string" 18 | assertEqual(mte.Error(), check, "Test_UnsupportedTypeError") 19 | } 20 | 21 | func Test_SyntaxError(t *testing.T) { 22 | assertEqual := cryptobin_test.AssertEqualT(t) 23 | 24 | mte := &SyntaxError{ 25 | Msg: "test msg", 26 | } 27 | 28 | check := "asn1: syntax error: test msg" 29 | assertEqual(mte.Error(), check, "Test_SyntaxError") 30 | } 31 | 32 | func Test_StructuralError(t *testing.T) { 33 | assertEqual := cryptobin_test.AssertEqualT(t) 34 | 35 | mte := &StructuralError{ 36 | Msg: "test msg", 37 | } 38 | 39 | check := "asn1: structure error: test msg" 40 | assertEqual(mte.Error(), check, "Test_StructuralError") 41 | } 42 | -------------------------------------------------------------------------------- /ber/asn1/null.go: -------------------------------------------------------------------------------- 1 | package asn1 2 | 3 | import "reflect" 4 | 5 | var nullType = reflect.TypeOf(Null{}) 6 | 7 | type Null struct{} 8 | 9 | type nullEncoder Null 10 | 11 | func (b nullEncoder) length() int { 12 | return 1 13 | } 14 | 15 | func (e nullEncoder) encode() ([]byte, error) { 16 | return nil, nil 17 | } 18 | 19 | -------------------------------------------------------------------------------- /ber/asn1/real.go: -------------------------------------------------------------------------------- 1 | package asn1 2 | 3 | import ( 4 | "bytes" 5 | "math" 6 | "strconv" 7 | ) 8 | 9 | type realEncoder float64 10 | 11 | func (b realEncoder) length() int { 12 | return 2 13 | } 14 | 15 | func (e realEncoder) encode() ([]byte, error) { 16 | // ECMA-63 17 | // https://www.ecma-international.org/wp-content/uploads/ECMA-63_1st_edition_september_1980.pdf 18 | 19 | n := float64(e) 20 | 21 | switch { 22 | case math.IsInf(n, 1): 23 | return []byte{0x40}, nil 24 | case math.IsInf(n, -1): 25 | return []byte{0x41}, nil 26 | case math.IsNaN(n): 27 | return []byte{0x42}, nil 28 | case n == 0.0: 29 | if math.Signbit(n) { 30 | return []byte{0x43}, nil 31 | } 32 | } 33 | 34 | nString := []byte(strconv.FormatFloat(n, 'G', -1, 64)) 35 | 36 | var buf []byte 37 | if bytes.Contains(nString, []byte{'E'}) { 38 | buf = []byte{0x03} 39 | } else { 40 | buf = []byte{0x02} 41 | } 42 | 43 | buf = append(buf, nString...) 44 | return buf, nil 45 | } 46 | -------------------------------------------------------------------------------- /ber/asn1/time.go: -------------------------------------------------------------------------------- 1 | package asn1 2 | 3 | import ( 4 | "reflect" 5 | "time" 6 | ) 7 | 8 | var timeType = reflect.TypeOf(time.Time{}) 9 | 10 | func makeUTCTime(t time.Time) string { 11 | // https://datatracker.ietf.org/doc/html/rfc5280#section-4.1.2.5.1 12 | return time.Time(t).Format("060102150405-0700") 13 | } 14 | 15 | func makeGeneralizedTime(t time.Time) string { 16 | // https://datatracker.ietf.org/doc/html/rfc5280#section-4.1.2.5.2 17 | return time.Time(t).Format("20060102150405Z") 18 | } 19 | -------------------------------------------------------------------------------- /ber/asn1/uint.go: -------------------------------------------------------------------------------- 1 | package asn1 2 | 3 | func encodeUint(n uint) []byte { 4 | length := uintLength(n) 5 | buf := make([]byte, length) 6 | for i := 0; i < length; i++ { 7 | shift := (length - 1 - i) * 8 8 | buf[i] = byte(n >> shift) 9 | } 10 | 11 | return buf 12 | } 13 | 14 | func uintLength(n uint) (length int) { 15 | length = 1 16 | for n > 255 { 17 | length++ 18 | n >>= 8 19 | } 20 | return 21 | } 22 | -------------------------------------------------------------------------------- /cipher/anubis/utils.go: -------------------------------------------------------------------------------- 1 | package anubis 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func getu32(ptr []byte) uint32 { 8 | return binary.BigEndian.Uint32(ptr) 9 | } 10 | 11 | func putu32(ptr []byte, a uint32) { 12 | binary.BigEndian.PutUint32(ptr, a) 13 | } 14 | 15 | func getu32Bytes(a uint32) []byte { 16 | var out [4]byte 17 | putu32(out[:], a) 18 | 19 | return out[:] 20 | } 21 | 22 | func bytesToUint32s(b []byte) []uint32 { 23 | size := len(b) / 4 24 | dst := make([]uint32, size) 25 | 26 | for i := 0; i < size; i++ { 27 | j := i * 4 28 | 29 | dst[i] = binary.BigEndian.Uint32(b[j:]) 30 | } 31 | 32 | return dst 33 | } 34 | 35 | func uint32sToBytes(w []uint32) []byte { 36 | size := len(w) * 4 37 | dst := make([]byte, size) 38 | 39 | for i := 0; i < len(w); i++ { 40 | j := i * 4 41 | 42 | binary.BigEndian.PutUint32(dst[j:], w[i]) 43 | } 44 | 45 | return dst 46 | } 47 | 48 | func uint32sToByteArray(w []uint32) (out [][]byte) { 49 | for _, v := range w { 50 | out = append(out, getu32Bytes(v)) 51 | } 52 | 53 | return 54 | } 55 | -------------------------------------------------------------------------------- /cipher/anubis2/utils.go: -------------------------------------------------------------------------------- 1 | package anubis2 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func getu32(ptr []byte) uint32 { 8 | return binary.BigEndian.Uint32(ptr) 9 | } 10 | 11 | func putu32(ptr []byte, a uint32) { 12 | binary.BigEndian.PutUint32(ptr, a) 13 | } 14 | 15 | func getu32Bytes(a uint32) []byte { 16 | var out [4]byte 17 | putu32(out[:], a) 18 | 19 | return out[:] 20 | } 21 | 22 | func bytesToUint32s(b []byte) []uint32 { 23 | size := len(b) / 4 24 | dst := make([]uint32, size) 25 | 26 | for i := 0; i < size; i++ { 27 | j := i * 4 28 | 29 | dst[i] = binary.BigEndian.Uint32(b[j:]) 30 | } 31 | 32 | return dst 33 | } 34 | 35 | func uint32sToBytes(w []uint32) []byte { 36 | size := len(w) * 4 37 | dst := make([]byte, size) 38 | 39 | for i := 0; i < len(w); i++ { 40 | j := i * 4 41 | 42 | binary.BigEndian.PutUint32(dst[j:], w[i]) 43 | } 44 | 45 | return dst 46 | } 47 | 48 | func uint32sToByteArray(w []uint32) (out [][]byte) { 49 | for _, v := range w { 50 | out = append(out, getu32Bytes(v)) 51 | } 52 | 53 | return 54 | } 55 | -------------------------------------------------------------------------------- /cipher/belt/utils.go: -------------------------------------------------------------------------------- 1 | package belt 2 | 3 | import ( 4 | "math/bits" 5 | "encoding/binary" 6 | ) 7 | 8 | func getu32(ptr []byte) uint32 { 9 | return binary.LittleEndian.Uint32(ptr) 10 | } 11 | 12 | func putu32(ptr []byte, a uint32) { 13 | binary.LittleEndian.PutUint32(ptr, a) 14 | } 15 | 16 | func rotatel32(x uint32, n int) uint32 { 17 | return bits.RotateLeft32(x, n) 18 | } 19 | 20 | func rotater32(x uint32, n int) uint32 { 21 | return rotatel32(x, 32 - n) 22 | } 23 | 24 | func ROTL_BELT(x uint32, n int) uint32 { 25 | return rotater32(x, n) 26 | } 27 | 28 | func GET_BYTE(x uint32, a int) byte { 29 | return byte((x >> a) & 0xff) 30 | } 31 | 32 | func PUT_BYTE(x uint32, a int) uint32 { 33 | return x << a 34 | } 35 | 36 | func SB(x uint32, a int) uint32 { 37 | return PUT_BYTE(uint32(S[GET_BYTE(x, a)]), a) 38 | } 39 | 40 | func G(x uint32, r int) uint32 { 41 | return ROTL_BELT(SB(x, 24) | SB(x, 16) | SB(x, 8) | SB(x, 0), r) 42 | } 43 | -------------------------------------------------------------------------------- /cipher/des/des_test.go: -------------------------------------------------------------------------------- 1 | package des 2 | 3 | import ( 4 | "testing" 5 | 6 | cryptobin_test "github.com/deatil/go-cryptobin/tool/test" 7 | ) 8 | 9 | func Test_Encrypt(t *testing.T) { 10 | assertEqual := cryptobin_test.AssertEqualT(t) 11 | assertNoError := cryptobin_test.AssertNoErrorT(t) 12 | assertNotEmpty := cryptobin_test.AssertNotEmptyT(t) 13 | 14 | key := "ujikioinsdujfic8" 15 | data := "ji9okind" 16 | 17 | dst := make([]byte, len(data)) 18 | 19 | cip, err := NewTwoDESCipher([]byte(key)) 20 | assertNoError(err, "Encrypt") 21 | 22 | cip.Encrypt(dst, []byte(data)) 23 | assertNotEmpty(dst, "Encrypt-Encrypt") 24 | 25 | newData := make([]byte, len(dst)) 26 | 27 | cip.Decrypt(newData, dst) 28 | assertNotEmpty(dst, "Encrypt-Decrypt") 29 | 30 | assertEqual(string(newData), data, "Encrypt") 31 | 32 | } 33 | -------------------------------------------------------------------------------- /cipher/hc/hc.go: -------------------------------------------------------------------------------- 1 | package hc 2 | 3 | import ( 4 | "strconv" 5 | "crypto/cipher" 6 | ) 7 | 8 | type KeySizeError int 9 | 10 | func (k KeySizeError) Error() string { 11 | return "go-cryptobin/hc: invalid key size " + strconv.Itoa(int(k)) 12 | } 13 | 14 | type IVSizeError int 15 | 16 | func (k IVSizeError) Error() string { 17 | return "go-cryptobin/hc: invalid iv size " + strconv.Itoa(int(k)) 18 | } 19 | 20 | // NewCipher creates and returns a new cipher.Block. 21 | func NewCipher(key, iv []byte) (cipher.Stream, error) { 22 | k := len(key) 23 | switch k { 24 | case 16: 25 | return NewCipher128(key, iv) 26 | case 32: 27 | return NewCipher256(key, iv) 28 | } 29 | 30 | return nil, KeySizeError(k) 31 | } 32 | -------------------------------------------------------------------------------- /cipher/kalyna/kalyna.go: -------------------------------------------------------------------------------- 1 | package kalyna 2 | 3 | import ( 4 | "fmt" 5 | "crypto/cipher" 6 | ) 7 | 8 | type KeySizeError int 9 | 10 | func (k KeySizeError) Error() string { 11 | return fmt.Sprintf("go-cryptobin/kalyna: invalid key size %d", int(k)) 12 | } 13 | 14 | // NewCipher creates and returns a new cipher.Block. 15 | func NewCipher(key []byte) (cipher.Block, error) { 16 | k := len(key) 17 | switch k { 18 | case 16: 19 | return NewCipher128_128(key) 20 | case 32: 21 | return NewCipher256_256(key) 22 | case 64: 23 | return NewCipher512_512(key) 24 | } 25 | 26 | return nil, KeySizeError(len(key)) 27 | } 28 | -------------------------------------------------------------------------------- /cipher/kasumi/utils.go: -------------------------------------------------------------------------------- 1 | package kasumi 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func bytesToUint32(inp []byte) (blk uint32) { 8 | blk = binary.BigEndian.Uint32(inp[0:]) 9 | 10 | return 11 | } 12 | 13 | func uint32ToBytes(blk uint32) []byte { 14 | var sav [4]byte 15 | 16 | binary.BigEndian.PutUint32(sav[0:], blk) 17 | 18 | return sav[:] 19 | } 20 | 21 | func ROL16(a, b uint16) uint16 { 22 | return (a << b) | (a >> (16 - b)) 23 | } 24 | -------------------------------------------------------------------------------- /cipher/loki97/ulong64.go: -------------------------------------------------------------------------------- 1 | package loki97 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | type ULONG64 struct { 8 | l uint32 9 | r uint32 10 | } 11 | 12 | func add64(a ULONG64, b ULONG64) ULONG64 { 13 | var sum ULONG64 14 | 15 | sum.r = a.r + b.r 16 | sum.l = a.l + b.l 17 | 18 | if sum.r < b.r { 19 | sum.l++ 20 | } 21 | 22 | return sum 23 | } 24 | 25 | func sub64(a ULONG64, b ULONG64) ULONG64 { 26 | var diff ULONG64 27 | 28 | diff.r = a.r - b.r 29 | diff.l = a.l - b.l 30 | 31 | if diff.r > a.r { 32 | diff.l-- 33 | } 34 | 35 | return diff 36 | } 37 | 38 | func byteToULONG64(inp []byte) ULONG64 { 39 | var I ULONG64 40 | 41 | I.l = binary.BigEndian.Uint32(inp[0:]) 42 | I.r = binary.BigEndian.Uint32(inp[4:]) 43 | 44 | return I 45 | } 46 | 47 | func ULONG64ToBYTE(I ULONG64) [8]byte { 48 | var sav [8]byte 49 | 50 | binary.BigEndian.PutUint32(sav[0:], I.l) 51 | binary.BigEndian.PutUint32(sav[4:], I.r) 52 | 53 | return sav 54 | } 55 | -------------------------------------------------------------------------------- /cipher/loki97/ulong64_test.go: -------------------------------------------------------------------------------- 1 | package loki97 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | "math/rand" 7 | ) 8 | 9 | func TestULONG64(t *testing.T) { 10 | random := rand.New(rand.NewSource(99)) 11 | 12 | key := make([]byte, 8) 13 | random.Read(key) 14 | 15 | en := byteToULONG64(key) 16 | de := ULONG64ToBYTE(en) 17 | 18 | if !bytes.Equal(de[:], key[:]) { 19 | t.Errorf("byteToULONG64/ULONG64ToBYTE failed: % 02x != % 02x\n", de, key) 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /cipher/magma/magma_test.go: -------------------------------------------------------------------------------- 1 | package magma 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | ) 7 | 8 | func Test_Check(t *testing.T) { 9 | key := []byte{ 10 | 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 11 | 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00, 12 | 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 13 | 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 14 | } 15 | 16 | pt := []byte{0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10} 17 | ct := []byte{0x4e, 0xe9, 0x01, 0xe5, 0xc2, 0xd8, 0xca, 0x3d} 18 | 19 | c, _ := NewCipher(key) 20 | 21 | dst := make([]byte, BlockSize) 22 | c.Encrypt(dst, pt[:]) 23 | if !bytes.Equal(dst, ct[:]) { 24 | t.Errorf("fail, got %x, want %x", dst, ct) 25 | } 26 | 27 | c.Decrypt(dst, dst) 28 | if !bytes.Equal(dst, pt[:]) { 29 | t.Errorf("fail, got %x, want %x", dst, pt) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /cipher/misty1/utils.go: -------------------------------------------------------------------------------- 1 | package misty1 2 | 3 | func fi(fin, fkey uint16) uint16 { 4 | d9 := fin >> 7 5 | d7 := fin & 0x7f 6 | d9 = s9table[d9] ^ d7 7 | d7 = s7table[d7] ^ d9 8 | d7 = d7 & 0x7f 9 | d7 = d7 ^ (fkey >> 9) 10 | d9 = d9 ^ (fkey & 0x1ff) 11 | d9 = s9table[d9] ^ d7 12 | fout := (d7 << 9) | d9 13 | return fout 14 | } 15 | 16 | // big-endian 17 | 18 | func getUint32(b []byte) uint32 { 19 | return uint32(b[3]) | 20 | uint32(b[2]) << 8 | 21 | uint32(b[1]) << 16 | 22 | uint32(b[0]) << 24 23 | } 24 | 25 | func putUint32(b []byte, v uint32) { 26 | b[0] = byte(v >> 24) 27 | b[1] = byte(v >> 16) 28 | b[2] = byte(v >> 8) 29 | b[3] = byte(v) 30 | } 31 | -------------------------------------------------------------------------------- /cipher/panama/struct.go: -------------------------------------------------------------------------------- 1 | package panama 2 | 3 | type IState interface { 4 | With(i uint32, word uint32) 5 | Get(i uint32) (word uint32) 6 | } 7 | 8 | type PAN_STAGE struct { 9 | word [PAN_STAGE_SIZE]uint32 10 | } 11 | 12 | func (this *PAN_STAGE) With(i uint32, word uint32) { 13 | this.word[i] = word 14 | } 15 | 16 | func (this *PAN_STAGE) Get(i uint32) (word uint32) { 17 | return this.word[i] 18 | } 19 | 20 | type PAN_BUFFER struct { 21 | stage [PAN_STAGES]PAN_STAGE 22 | tap_0 int32 23 | } 24 | 25 | type PAN_STATE struct { 26 | word [PAN_STATE_SIZE]uint32 27 | } 28 | 29 | func (this *PAN_STATE) With(i uint32, word uint32) { 30 | this.word[i] = word 31 | } 32 | 33 | func (this *PAN_STATE) Get(i uint32) (word uint32) { 34 | return this.word[i] 35 | } 36 | 37 | -------------------------------------------------------------------------------- /cipher/present/utils.go: -------------------------------------------------------------------------------- 1 | package present 2 | 3 | import ( 4 | "math/bits" 5 | "encoding/binary" 6 | ) 7 | 8 | func bytesToUint64(inp []byte) (blk uint64) { 9 | blk = binary.BigEndian.Uint64(inp[0:]) 10 | 11 | return 12 | } 13 | 14 | func uint64ToBytes(blk uint64) []byte { 15 | var sav [8]byte 16 | 17 | binary.BigEndian.PutUint64(sav[0:], blk) 18 | 19 | return sav[:] 20 | } 21 | 22 | func bytesToUint64s(b []byte) []uint64 { 23 | size := len(b) / 8 24 | dst := make([]uint64, size) 25 | 26 | for i := 0; i < size; i++ { 27 | j := i * 8 28 | 29 | dst[i] = binary.BigEndian.Uint64(b[j:]) 30 | } 31 | 32 | return dst 33 | } 34 | 35 | func uint64sToBytes(w []uint64) []byte { 36 | size := len(w) * 8 37 | dst := make([]byte, size) 38 | 39 | for i := 0; i < len(w); i++ { 40 | j := i * 8 41 | 42 | binary.BigEndian.PutUint64(dst[j:], w[i]) 43 | } 44 | 45 | return dst 46 | } 47 | 48 | func ROL64(x, n uint64) uint64 { 49 | return bits.RotateLeft64(x, int(n)) 50 | } 51 | -------------------------------------------------------------------------------- /cipher/rabbit/sbox.go: -------------------------------------------------------------------------------- 1 | package rabbit 2 | 3 | var aro = []uint32{ 4 | 0x4D34D34D, 0xD34D34D3, 0x34D34D34, 0x4D34D34D, 5 | 0xD34D34D3, 0x34D34D34, 0x4D34D34D, 0xD34D34D3, 6 | } 7 | -------------------------------------------------------------------------------- /cipher/rabbit/utils.go: -------------------------------------------------------------------------------- 1 | package rabbit 2 | 3 | import ( 4 | "math/bits" 5 | "encoding/binary" 6 | ) 7 | 8 | func getu16(ptr []byte) uint16 { 9 | return binary.LittleEndian.Uint16(ptr) 10 | } 11 | 12 | func getu32(ptr []byte) uint32 { 13 | return binary.LittleEndian.Uint32(ptr) 14 | } 15 | 16 | func putu32(ptr []byte, a uint32) { 17 | binary.LittleEndian.PutUint32(ptr, a) 18 | } 19 | 20 | func rol32(x uint32, n int) uint32 { 21 | return bits.RotateLeft32(x, n) 22 | } 23 | 24 | func gfunction(u, v uint32) uint32 { 25 | uv := uint64(u) + uint64(v) 26 | uv *= uv 27 | return uint32(uv>>32) ^ uint32(uv) 28 | } 29 | -------------------------------------------------------------------------------- /cipher/rc2/rc2_test.go: -------------------------------------------------------------------------------- 1 | package rc2 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | "math/rand" 7 | ) 8 | 9 | func Test_Lea_Key16(t *testing.T) { 10 | random := rand.New(rand.NewSource(99)) 11 | max := 5000 12 | 13 | var encrypted [8]byte 14 | var decrypted [8]byte 15 | 16 | for i := 0; i < max; i++ { 17 | key := make([]byte, 16) 18 | random.Read(key) 19 | value := make([]byte, 8) 20 | random.Read(value) 21 | 22 | cipher1, err := NewCipher(key, len(key)*8) 23 | if err != nil { 24 | t.Fatal(err.Error()) 25 | } 26 | 27 | cipher1.Encrypt(encrypted[:], value) 28 | 29 | cipher2, err := NewCipher(key, len(key)*8) 30 | if err != nil { 31 | t.Fatal(err.Error()) 32 | } 33 | 34 | cipher2.Decrypt(decrypted[:], encrypted[:]) 35 | 36 | if !bytes.Equal(decrypted[:], value[:]) { 37 | t.Errorf("encryption/decryption failed: % 02x != % 02x\n", decrypted, value) 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /cipher/seed/seed.go: -------------------------------------------------------------------------------- 1 | package seed 2 | 3 | import ( 4 | "fmt" 5 | "crypto/cipher" 6 | ) 7 | 8 | // Package seed implements SEED encryption, as defined in TTAS.KO-12.0004/R1 9 | 10 | const BlockSize = 16 11 | 12 | type KeySizeError int 13 | 14 | func (k KeySizeError) Error() string { 15 | return fmt.Sprintf("go-cryptobin/seed: invalid key size %d", int(k)) 16 | } 17 | 18 | // NewCipher creates and returns a new cipher.Block. The key argument should be the SEED key, either 16 or 32 bytes to select SEED-128 or SEED-256. 19 | func NewCipher(key []byte) (cipher.Block, error) { 20 | k := len(key) 21 | switch k { 22 | case 16: 23 | return newSeed128Cipher(key), nil 24 | case 32: 25 | return newSeed256Cipher(key), nil 26 | } 27 | 28 | return nil, KeySizeError(k) 29 | } 30 | -------------------------------------------------------------------------------- /cipher/seed/utils.go: -------------------------------------------------------------------------------- 1 | package seed 2 | 3 | func g(n uint32) uint32 { 4 | return ss0[0xFF&(n>>0)] ^ ss1[0xFF&(n>>8)] ^ ss2[0xFF&(n>>16)] ^ ss3[0xFF&(n>>24)] 5 | } 6 | 7 | func processBlock(t0, t1 uint32) (uint32, uint32) { 8 | t1 ^= t0 9 | t1 = g(t1) 10 | t0 += t1 11 | t0 = g(t0) 12 | t1 += t0 13 | t1 = g(t1) 14 | t0 += t1 15 | 16 | return t0, t1 17 | } 18 | -------------------------------------------------------------------------------- /cipher/shacal2/sbox.go: -------------------------------------------------------------------------------- 1 | package shacal2 2 | 3 | var rc = [64]uint32{ 4 | 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 5 | 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 6 | 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 7 | 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967, 8 | 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 9 | 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 10 | 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 11 | 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2, 12 | } 13 | -------------------------------------------------------------------------------- /cipher/simon/simon.go: -------------------------------------------------------------------------------- 1 | package simon 2 | 3 | import ( 4 | "fmt" 5 | "crypto/cipher" 6 | ) 7 | 8 | const BlockSize = 16 9 | 10 | type KeySizeError int 11 | 12 | func (k KeySizeError) Error() string { 13 | return fmt.Sprintf("go-cryptobin/simon: invalid key size %d", int(k)) 14 | } 15 | 16 | // NewCipher creates and returns a new cipher.Block. 17 | // simon128 18 | func NewCipher(key []byte) (cipher.Block, error) { 19 | k := len(key) 20 | switch k { 21 | case 16: 22 | return NewCipher128(key) 23 | case 24: 24 | return NewCipher192(key) 25 | case 32: 26 | return NewCipher256(key) 27 | } 28 | 29 | return nil, KeySizeError(k) 30 | } 31 | -------------------------------------------------------------------------------- /cipher/skein/sbox.go: -------------------------------------------------------------------------------- 1 | package skein 2 | 3 | const streamOutLen = (1<<64 - 1) / 8 // 2^64 - 1 bits 4 | 5 | // Argument types (in the order they must be used). 6 | const ( 7 | keyArg uint64 = 0 8 | configArg uint64 = 4 9 | keyIDArg uint64 = 16 10 | nonceArg uint64 = 20 11 | messageArg uint64 = 48 12 | outputArg uint64 = 63 13 | ) 14 | 15 | const ( 16 | firstBlockFlag uint64 = 1 << 62 17 | lastBlockFlag uint64 = 1 << 63 18 | ) 19 | 20 | var schemaId = []byte{'S', 'H', 'A', '3', 1, 0, 0, 0} 21 | var outTweak = [2]uint64{8, outputArg<<56 | firstBlockFlag | lastBlockFlag} 22 | -------------------------------------------------------------------------------- /cipher/skipjack/skipjack_test.go: -------------------------------------------------------------------------------- 1 | package skipjack 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | "math/rand" 7 | ) 8 | 9 | func Test_Skipjack(t *testing.T) { 10 | random := rand.New(rand.NewSource(99)) 11 | max := 5000 12 | 13 | var encrypted [8]byte 14 | var decrypted [8]byte 15 | 16 | for i := 0; i < max; i++ { 17 | key := make([]byte, 10) 18 | random.Read(key) 19 | value := make([]byte, 8) 20 | random.Read(value) 21 | 22 | cipher, err := NewCipher(key) 23 | if err != nil { 24 | t.Fatal(err.Error()) 25 | } 26 | 27 | cipher.Encrypt(encrypted[:], value) 28 | 29 | if bytes.Equal(encrypted[:], value[:]) { 30 | t.Errorf("fail: encrypted equal value\n") 31 | } 32 | 33 | cipher.Decrypt(decrypted[:], encrypted[:]) 34 | 35 | if !bytes.Equal(decrypted[:], value[:]) { 36 | t.Errorf("encryption/decryption failed: % 02x != % 02x\n", decrypted, value) 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /cipher/speck/utils.go: -------------------------------------------------------------------------------- 1 | package speck 2 | 3 | import ( 4 | "math/bits" 5 | "encoding/binary" 6 | ) 7 | 8 | func keyToUint64s(b []byte) []uint64 { 9 | size := len(b) / 8 10 | dst := make([]uint64, size) 11 | 12 | for i := 0; i < size; i++ { 13 | j := i * 8 14 | 15 | dst[i] = binary.LittleEndian.Uint64(b[j:]) 16 | } 17 | 18 | return dst 19 | } 20 | 21 | func uint64sToBytes(w []uint64) []byte { 22 | size := len(w) * 8 23 | dst := make([]byte, size) 24 | 25 | for i := 0; i < len(w); i++ { 26 | j := i * 8 27 | 28 | binary.LittleEndian.PutUint64(dst[j:], w[i]) 29 | } 30 | 31 | return dst 32 | } 33 | 34 | func rotatel64(x uint64, n int) uint64 { 35 | return bits.RotateLeft64(x, n) 36 | } 37 | 38 | func rotater64(x uint64, n int) uint64 { 39 | return rotatel64(x, 64 - n) 40 | } 41 | 42 | func ks(x uint64, y *uint64, pk uint64, nk *uint64, i uint64) { 43 | (*y) = (pk + rotater64(x, 8)) ^ i 44 | (*nk) = rotatel64(pk, 3) ^ (*y) 45 | } 46 | -------------------------------------------------------------------------------- /cipher/spritz/hash.go: -------------------------------------------------------------------------------- 1 | package spritz 2 | 3 | func Hash(m []byte, r byte) []byte { 4 | var c spritzCipher 5 | 6 | c.initializeState() 7 | 8 | c.absorb(m) 9 | c.absorbStop() 10 | 11 | // hash length 12 | c.absorbByte(r) 13 | 14 | return c.squeeze(int(r)) 15 | } 16 | -------------------------------------------------------------------------------- /cipher/trivium/utils.go: -------------------------------------------------------------------------------- 1 | package trivium 2 | 3 | func reverseInt8(value uint8) uint8 { 4 | value = ((value & 0xF0) >> 4) | ((value & 0x0F) << 4) 5 | value = ((value & 0xCC) >> 2) | ((value & 0x33) << 2) 6 | value = ((value & 0xAA) >> 1) | ((value & 0x55) << 1) 7 | 8 | return value; 9 | } 10 | 11 | func TRIVIUM_GET_BIT(s []uint8, n int) uint8 { 12 | return ((s[(n - 1) / 8] >> ((n - 1) % 8)) & 1) 13 | } 14 | 15 | //Set a given bit of the internal state 16 | func TRIVIUM_SET_BIT(s []uint8, n int, v uint8) { 17 | s[(n - 1) / 8] = (s[(n - 1) / 8] & ^(1 << ((n - 1) % 8))) | (v << ((n - 1) % 8)) 18 | } 19 | -------------------------------------------------------------------------------- /cipher/twine/sbox.go: -------------------------------------------------------------------------------- 1 | package twine 2 | 3 | // table 1 4 | var sbox = []byte{ 5 | 0x0C, 0x00, 0x0F, 0x0A, 0x02, 0x0B, 0x09, 0x05, 6 | 0x08, 0x03, 0x0D, 0x07, 0x01, 0x0E, 0x06, 0x04, 7 | } 8 | 9 | // table 2 10 | var shuf = []int{ 11 | 5, 0, 1, 4, 7, 12, 3, 8, 12 | 13, 6, 9, 2, 15, 10, 11, 14, 13 | } 14 | 15 | var shufinv = []int{ 16 | 1, 2, 11, 6, 3, 0, 9, 4, 17 | 7, 10, 13, 14, 5, 8, 15, 12, 18 | } 19 | 20 | // table 3 21 | var roundconst = []byte{ 22 | 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x03, 0x06, 0x0c, 0x18, 0x30, 0x23, 0x05, 0x0a, 0x14, 0x28, 0x13, 0x26, 23 | 0x0f, 0x1e, 0x3c, 0x3b, 0x35, 0x29, 0x11, 0x22, 0x07, 0x0e, 0x1c, 0x38, 0x33, 0x25, 0x09, 0x12, 0x24, 0x0b, 24 | } 25 | -------------------------------------------------------------------------------- /cipher/wake/utils.go: -------------------------------------------------------------------------------- 1 | package wake 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func bytesToUint32s(inp []byte) [4]uint32 { 8 | var blk [4]uint32 9 | 10 | blk[0] = binary.BigEndian.Uint32(inp[0:]) 11 | blk[1] = binary.BigEndian.Uint32(inp[4:]) 12 | blk[2] = binary.BigEndian.Uint32(inp[8:]) 13 | blk[3] = binary.BigEndian.Uint32(inp[12:]) 14 | 15 | return blk 16 | } 17 | 18 | func Uint32sToBytes(blk [4]uint32) [16]byte { 19 | var sav [16]byte 20 | 21 | binary.BigEndian.PutUint32(sav[0:], blk[0]) 22 | binary.BigEndian.PutUint32(sav[4:], blk[1]) 23 | binary.BigEndian.PutUint32(sav[8:], blk[2]) 24 | binary.BigEndian.PutUint32(sav[12:], blk[3]) 25 | 26 | return sav 27 | } 28 | 29 | func bytesToUint32(inp []byte) (blk uint32) { 30 | blk = binary.BigEndian.Uint32(inp[0:]) 31 | 32 | return 33 | } 34 | 35 | func uint32ToBytes(blk uint32) [4]byte { 36 | var sav [4]byte 37 | 38 | binary.BigEndian.PutUint32(sav[0:], blk) 39 | 40 | return sav 41 | } 42 | -------------------------------------------------------------------------------- /cipher/xoodoo/xoodoo/example_test.go: -------------------------------------------------------------------------------- 1 | package xoodoo_test 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/deatil/go-cryptobin/cipher/xoodoo/xoodoo" 7 | ) 8 | 9 | func Example() { 10 | newXoodoo, _ := xoodoo.NewXoodoo(xoodoo.MaxRounds, [xoodoo.StateSizeBytes]byte{}) 11 | fmt.Printf("Starting State:%#v\n", newXoodoo.Bytes()) 12 | newXoodoo.Permutation() 13 | fmt.Printf("Permuted State:%#v\n", newXoodoo.Bytes()) 14 | // Output: Starting State:[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} 15 | // Permuted State:[]byte{0x8d, 0xd8, 0xd5, 0x89, 0xbf, 0xfc, 0x63, 0xa9, 0x19, 0x2d, 0x23, 0x1b, 0x14, 0xa0, 0xa5, 0xff, 0x6, 0x81, 0xb1, 0x36, 0xfe, 0xc1, 0xc7, 0xaf, 0xbe, 0x7c, 0xe5, 0xae, 0xbd, 0x40, 0x75, 0xa7, 0x70, 0xe8, 0x86, 0x2e, 0xc9, 0xb7, 0xf5, 0xfe, 0xf2, 0xad, 0x4f, 0x8b, 0x62, 0x40, 0x4f, 0x5e} 16 | } 17 | -------------------------------------------------------------------------------- /cipher/xoodoo/xoodoo/xoodoo_test.go: -------------------------------------------------------------------------------- 1 | package xoodoo_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/deatil/go-cryptobin/cipher/xoodoo/xoodoo" 7 | ) 8 | 9 | func Test_NewXoodoo(t *testing.T) { 10 | newXoodoo, _ := xoodoo.NewXoodoo(xoodoo.MaxRounds, [xoodoo.StateSizeBytes]byte{}) 11 | 12 | if len(newXoodoo.Bytes()) == 0 { 13 | t.Error("NewXoodoo fail") 14 | } 15 | 16 | newXoodoo.Permutation() 17 | 18 | if len(newXoodoo.Bytes()) == 0 { 19 | t.Error("after Permutation and NewXoodoo fail") 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /cipher/xoodoo/xoodyak/mac.go: -------------------------------------------------------------------------------- 1 | package xoodyak 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // NewXoodyakMac generates a new hashing object with the provided key data already baked in. Writing 8 | // Any data then written to the hash object is part of the MAC check. Note that the length of the 9 | // resulting MAC matches that of the official Xoodyak hash output: 32 bytes 10 | func NewXoodyakMac(key []byte) hash.Hash { 11 | d := &digest{absorbCd: AbsorbCdInit} 12 | xk := Instantiate(key, []byte{}, []byte{}) 13 | d.xk = xk 14 | d.x = make([]byte, xk.AbsorbSize) 15 | return d 16 | } 17 | 18 | // MACXoodyak generates a message authentication code of the desired length in bytes for the provided 19 | // message based on the provided key data. 20 | // This implements the MAC behavior described in section 1.3.2 of the Xoodyak specification. 21 | func MACXoodyak(key, msg []byte, macLen uint) []byte { 22 | xkMAC := Instantiate(key, nil, nil) 23 | xkMAC.Absorb(msg) 24 | return xkMAC.Squeeze(macLen) 25 | } 26 | -------------------------------------------------------------------------------- /cryptobin/bign/check.go: -------------------------------------------------------------------------------- 1 | package bign 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this Bign) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this.CreatePublicKey().ToKeyString() 12 | 13 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 14 | return false 15 | } 16 | 17 | if pubKeyFromPriKey == pubKeyFromPubKey { 18 | return true 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /cryptobin/bign/error.go: -------------------------------------------------------------------------------- 1 | package bign 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this Bign) AppendError(err ...error) Bign { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this Bign) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/bign/make.go: -------------------------------------------------------------------------------- 1 | package bign 2 | 3 | import( 4 | "errors" 5 | "encoding/pem" 6 | ) 7 | 8 | // 生成公钥 9 | func (this Bign) MakePublicKey() Bign { 10 | this.publicKey = nil 11 | 12 | if this.privateKey == nil { 13 | err := errors.New("privateKey empty.") 14 | return this.AppendError(err) 15 | } 16 | 17 | this.publicKey = &this.privateKey.PublicKey 18 | 19 | return this 20 | } 21 | 22 | // 生成密钥 der 数据 23 | func (this Bign) MakeKeyDer() Bign { 24 | var block *pem.Block 25 | if block, _ = pem.Decode(this.keyData); block == nil { 26 | err := errors.New("keyData error.") 27 | return this.AppendError(err) 28 | } 29 | 30 | this.keyData = block.Bytes 31 | 32 | return this 33 | } 34 | -------------------------------------------------------------------------------- /cryptobin/bign/on.go: -------------------------------------------------------------------------------- 1 | package bign 2 | 3 | // 引出错误信息 4 | func (this Bign) OnError(fn func([]error)) Bign { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/bign/to.go: -------------------------------------------------------------------------------- 1 | package bign 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 私钥/公钥 8 | func (this Bign) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // 私钥/公钥 13 | func (this Bign) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ========== 18 | 19 | // 输出字节 20 | func (this Bign) ToBytes() []byte { 21 | return this.parsedData 22 | } 23 | 24 | // 输出字符 25 | func (this Bign) ToString() string { 26 | return string(this.parsedData) 27 | } 28 | 29 | // 输出Base64 30 | func (this Bign) ToBase64String() string { 31 | return encoding.Base64Encode(this.parsedData) 32 | } 33 | 34 | // 输出Hex 35 | func (this Bign) ToHexString() string { 36 | return encoding.HexEncode(this.parsedData) 37 | } 38 | 39 | // ========== 40 | 41 | // 验证结果 42 | func (this Bign) ToVerify() bool { 43 | return this.verify 44 | } 45 | 46 | // 验证结果,返回 int 类型 47 | func (this Bign) ToVerifyInt() int { 48 | if this.verify { 49 | return 1 50 | } 51 | 52 | return 0 53 | } 54 | -------------------------------------------------------------------------------- /cryptobin/bip0340/check.go: -------------------------------------------------------------------------------- 1 | package bip0340 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this BIP0340) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this.CreatePublicKey().ToKeyString() 12 | 13 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 14 | return false 15 | } 16 | 17 | if pubKeyFromPriKey == pubKeyFromPubKey { 18 | return true 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /cryptobin/bip0340/error.go: -------------------------------------------------------------------------------- 1 | package bip0340 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this BIP0340) AppendError(err ...error) BIP0340 { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this BIP0340) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/bip0340/make.go: -------------------------------------------------------------------------------- 1 | package bip0340 2 | 3 | import( 4 | "errors" 5 | "encoding/pem" 6 | ) 7 | 8 | // 生成公钥 9 | func (this BIP0340) MakePublicKey() BIP0340 { 10 | this.publicKey = nil 11 | 12 | if this.privateKey == nil { 13 | err := errors.New("privateKey empty.") 14 | return this.AppendError(err) 15 | } 16 | 17 | this.publicKey = &this.privateKey.PublicKey 18 | 19 | return this 20 | } 21 | 22 | // 生成密钥 der 数据 23 | func (this BIP0340) MakeKeyDer() BIP0340 { 24 | var block *pem.Block 25 | if block, _ = pem.Decode(this.keyData); block == nil { 26 | err := errors.New("keyData error.") 27 | return this.AppendError(err) 28 | } 29 | 30 | this.keyData = block.Bytes 31 | 32 | return this 33 | } 34 | -------------------------------------------------------------------------------- /cryptobin/bip0340/on.go: -------------------------------------------------------------------------------- 1 | package bip0340 2 | 3 | // 引出错误信息 4 | func (this BIP0340) OnError(fn func([]error)) BIP0340 { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/bip0340/to.go: -------------------------------------------------------------------------------- 1 | package bip0340 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 私钥/公钥 8 | func (this BIP0340) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // 私钥/公钥 13 | func (this BIP0340) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ========== 18 | 19 | // 输出字节 20 | func (this BIP0340) ToBytes() []byte { 21 | return this.parsedData 22 | } 23 | 24 | // 输出字符 25 | func (this BIP0340) ToString() string { 26 | return string(this.parsedData) 27 | } 28 | 29 | // 输出Base64 30 | func (this BIP0340) ToBase64String() string { 31 | return encoding.Base64Encode(this.parsedData) 32 | } 33 | 34 | // 输出Hex 35 | func (this BIP0340) ToHexString() string { 36 | return encoding.HexEncode(this.parsedData) 37 | } 38 | 39 | // ========== 40 | 41 | // 验证结果 42 | func (this BIP0340) ToVerify() bool { 43 | return this.verify 44 | } 45 | 46 | // 验证结果,返回 int 类型 47 | func (this BIP0340) ToVerifyInt() int { 48 | if this.verify { 49 | return 1 50 | } 51 | 52 | return 0 53 | } 54 | -------------------------------------------------------------------------------- /cryptobin/ca/error.go: -------------------------------------------------------------------------------- 1 | package ca 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this CA) AppendError(err ...error) CA { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this CA) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/ca/on.go: -------------------------------------------------------------------------------- 1 | package ca 2 | 3 | // 引出错误信息 4 | func (this CA) OnError(fn func([]error)) CA { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/ca/to.go: -------------------------------------------------------------------------------- 1 | package ca 2 | 3 | // 私钥/公钥/cert 4 | func (this CA) ToKeyBytes() []byte { 5 | return this.keyData 6 | } 7 | 8 | // 私钥/公钥/cert 9 | func (this CA) ToKeyString() string { 10 | return string(this.keyData) 11 | } 12 | -------------------------------------------------------------------------------- /cryptobin/ca/verify.go: -------------------------------------------------------------------------------- 1 | package ca 2 | 3 | import ( 4 | "errors" 5 | "encoding/pem" 6 | 7 | "github.com/deatil/go-cryptobin/x509" 8 | ) 9 | 10 | // 验证 11 | func (this CA) Verify(rootPEM string, certPEM string, opts x509.VerifyOptions) (bool, error) { 12 | roots := x509.NewCertPool() 13 | 14 | ok := roots.AppendCertsFromPEM([]byte(rootPEM)) 15 | if !ok { 16 | return false, errors.New("failed to parse root certificate") 17 | } 18 | 19 | block, _ := pem.Decode([]byte(certPEM)) 20 | if block == nil { 21 | return false, errors.New("failed to parse certificate PEM") 22 | } 23 | 24 | cert, err := x509.ParseCertificate(block.Bytes) 25 | if err != nil { 26 | return false, errors.New("failed to parse certificate: " + err.Error()) 27 | } 28 | 29 | // 重设 30 | opts.Roots = roots 31 | 32 | if _, err := cert.Verify(opts); err != nil { 33 | return false, errors.New("failed to verify certificate: " + err.Error()) 34 | } 35 | 36 | return true, nil 37 | } 38 | -------------------------------------------------------------------------------- /cryptobin/crypto/config.go: -------------------------------------------------------------------------------- 1 | package crypto 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/config" 5 | ) 6 | 7 | /** 8 | * 配置 / Config 9 | * 10 | * @create 2023-3-30 11 | * @author deatil 12 | */ 13 | type Config struct { 14 | crypto Cryptobin 15 | } 16 | 17 | // New Config 18 | func NewConfig(c Cryptobin) Config { 19 | return Config{ 20 | crypto: c, 21 | } 22 | } 23 | 24 | // 获取密钥 25 | // get Key 26 | func (this Config) Key() []byte { 27 | return this.crypto.key 28 | } 29 | 30 | // 获取向量 31 | // get Iv 32 | func (this Config) Iv() []byte { 33 | return this.crypto.iv 34 | } 35 | 36 | // 获取加密类型 37 | // get Multiple 38 | func (this Config) Multiple() Multiple { 39 | return this.crypto.multiple 40 | } 41 | 42 | // 获取加密模式 43 | // get Mode 44 | func (this Config) Mode() Mode { 45 | return this.crypto.mode 46 | } 47 | 48 | // 获取补码 49 | // get Padding 50 | func (this Config) Padding() Padding { 51 | return this.crypto.padding 52 | } 53 | 54 | // 获取额外配置 55 | // get extra Config 56 | func (this Config) Config() *config.Config { 57 | return this.crypto.config 58 | } 59 | -------------------------------------------------------------------------------- /cryptobin/crypto/encrypt/aescfb/aescfb_test.go: -------------------------------------------------------------------------------- 1 | package aescfb 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/deatil/go-cryptobin/tool/test" 7 | "github.com/deatil/go-cryptobin/cryptobin/crypto" 8 | ) 9 | 10 | func Test_Name(t *testing.T) { 11 | eq := test.AssertEqualT(t) 12 | 13 | eq(AesCFB.String(), "AesCFB", "Test_Name") 14 | } 15 | 16 | func Test_AesCFB(t *testing.T) { 17 | assert := test.AssertEqualT(t) 18 | assertNoError := test.AssertNoErrorT(t) 19 | 20 | data := "test-pass" 21 | cypt := crypto.FromString(data). 22 | SetKey("dfertf12dfertf12"). 23 | MultipleBy(AesCFB). 24 | Encrypt() 25 | cyptStr := cypt.ToBase64String() 26 | 27 | assertNoError(cypt.Error(), "AesCFB-Encode") 28 | 29 | cyptde := crypto.FromBase64String(cyptStr). 30 | SetKey("dfertf12dfertf12"). 31 | MultipleBy(AesCFB). 32 | Decrypt() 33 | cyptdeStr := cyptde.ToString() 34 | 35 | assertNoError(cyptde.Error(), "AesCFB-Decode") 36 | 37 | assert(data, cyptdeStr, "AesCFB") 38 | } 39 | -------------------------------------------------------------------------------- /cryptobin/crypto/encrypt/aesecb/aesecb_test.go: -------------------------------------------------------------------------------- 1 | package aesecb 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/deatil/go-cryptobin/tool/test" 7 | "github.com/deatil/go-cryptobin/cryptobin/crypto" 8 | ) 9 | 10 | func Test_Name(t *testing.T) { 11 | eq := test.AssertEqualT(t) 12 | 13 | eq(AesECB.String(), "AesECB", "Test_Name") 14 | } 15 | 16 | func Test_AesECB(t *testing.T) { 17 | assert := test.AssertEqualT(t) 18 | assertNoError := test.AssertNoErrorT(t) 19 | 20 | data := "test-pass" 21 | cypt := crypto.FromString(data). 22 | SetKey("dfertf12dfertf12rtgthytr"). 23 | MultipleBy(AesECB). 24 | Encrypt() 25 | cyptStr := cypt.ToBase64String() 26 | 27 | assertNoError(cypt.Error(), "AesECB-Encode") 28 | 29 | cyptde := crypto.FromBase64String(cyptStr). 30 | SetKey("dfertf12dfertf12rtgthytr"). 31 | MultipleBy(AesECB). 32 | Decrypt() 33 | cyptdeStr := cyptde.ToString() 34 | 35 | assertNoError(cyptde.Error(), "AesECB-Decode") 36 | 37 | assert(data, cyptdeStr, "AesECB") 38 | } 39 | -------------------------------------------------------------------------------- /cryptobin/crypto/encrypt/threeway/threeway_test.go: -------------------------------------------------------------------------------- 1 | package threeway 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/deatil/go-cryptobin/tool/test" 7 | "github.com/deatil/go-cryptobin/cryptobin/crypto" 8 | ) 9 | 10 | func Test_Name(t *testing.T) { 11 | eq := test.AssertEqualT(t) 12 | 13 | eq(Threeway.String(), "Threeway", "Test_Name") 14 | } 15 | 16 | func Test_ThreewayPKCS7Padding(t *testing.T) { 17 | eq := test.AssertEqualT(t) 18 | 19 | data := "test-pass" 20 | cyptStr := crypto.FromString(data). 21 | SetKey("dfertf12dfyy"). 22 | SetIv("dfertf12dfer"). 23 | MultipleBy(Threeway). 24 | CFB(). 25 | PKCS7Padding(). 26 | Encrypt(). 27 | ToBase64String() 28 | 29 | cyptdeStr := crypto.FromBase64String(cyptStr). 30 | SetKey("dfertf12dfyy"). 31 | SetIv("dfertf12dfer"). 32 | MultipleBy(Threeway). 33 | CFB(). 34 | PKCS7Padding(). 35 | Decrypt(). 36 | ToString() 37 | 38 | eq(data, cyptdeStr, "Test_ThreewayPKCS7Padding") 39 | } 40 | -------------------------------------------------------------------------------- /cryptobin/crypto/error.go: -------------------------------------------------------------------------------- 1 | package crypto 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | // Append Error 9 | func (this Cryptobin) AppendError(err ...error) Cryptobin { 10 | this.Errors = append(this.Errors, err...) 11 | 12 | return this 13 | } 14 | 15 | // 获取错误 16 | // get error 17 | func (this Cryptobin) Error() error { 18 | return errors.Join(this.Errors...) 19 | } 20 | -------------------------------------------------------------------------------- /cryptobin/crypto/on.go: -------------------------------------------------------------------------------- 1 | package crypto 2 | 3 | type ( 4 | // 错误方法 5 | // Error Func 6 | ErrorFunc = func([]error) 7 | ) 8 | 9 | // 错误事件 10 | // On Error 11 | func (this Cryptobin) OnError(fn ErrorFunc) Cryptobin { 12 | this.errEvent = this.errEvent.On(fn) 13 | 14 | return this 15 | } 16 | 17 | // 触发错误事件 18 | // trigger Error 19 | func (this Cryptobin) triggerError() Cryptobin { 20 | this.errEvent.Trigger(this.Errors) 21 | 22 | return this 23 | } 24 | 25 | -------------------------------------------------------------------------------- /cryptobin/crypto/to.go: -------------------------------------------------------------------------------- 1 | package crypto 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 输出原始字符 8 | // output String 9 | func (this Cryptobin) String() string { 10 | return string(this.data) 11 | } 12 | 13 | // 输出字节 14 | // output Bytes 15 | func (this Cryptobin) ToBytes() []byte { 16 | return this.parsedData 17 | } 18 | 19 | // 输出字符 20 | // output String 21 | func (this Cryptobin) ToString() string { 22 | return string(this.parsedData) 23 | } 24 | 25 | // 输出 Base64 26 | // output Base64 String 27 | func (this Cryptobin) ToBase64String() string { 28 | return encoding.Base64Encode(this.parsedData) 29 | } 30 | 31 | // 输出 Hex 32 | // output Hex String 33 | func (this Cryptobin) ToHexString() string { 34 | return encoding.HexEncode(this.parsedData) 35 | } 36 | -------------------------------------------------------------------------------- /cryptobin/dh/curve25519/check.go: -------------------------------------------------------------------------------- 1 | package curve25519 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this Curve25519) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this.CreatePublicKey().ToKeyString() 12 | 13 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 14 | return false 15 | } 16 | 17 | if pubKeyFromPriKey == pubKeyFromPubKey { 18 | return true 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /cryptobin/dh/curve25519/curve25519.go: -------------------------------------------------------------------------------- 1 | package curve25519 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/pubkey/dh/curve25519" 5 | ) 6 | 7 | /** 8 | * curve25519 9 | * 10 | * @create 2022-8-7 11 | * @author deatil 12 | */ 13 | type Curve25519 struct { 14 | // 私钥 15 | privateKey *curve25519.PrivateKey 16 | 17 | // 公钥 18 | publicKey *curve25519.PublicKey 19 | 20 | // [私钥/公钥]数据 21 | keyData []byte 22 | 23 | // 密码数据 24 | secretData []byte 25 | 26 | // 错误 27 | Errors []error 28 | } 29 | 30 | // 构造函数 31 | func NewCurve25519() Curve25519 { 32 | return Curve25519{ 33 | Errors: make([]error, 0), 34 | } 35 | } 36 | 37 | // 构造函数 38 | func New() Curve25519 { 39 | return NewCurve25519() 40 | } 41 | 42 | var ( 43 | // 默认 44 | defaultCurve25519 = NewCurve25519() 45 | ) 46 | -------------------------------------------------------------------------------- /cryptobin/dh/curve25519/error.go: -------------------------------------------------------------------------------- 1 | package curve25519 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this Curve25519) AppendError(err ...error) Curve25519 { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this Curve25519) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/dh/curve25519/make.go: -------------------------------------------------------------------------------- 1 | package curve25519 2 | 3 | import ( 4 | "errors" 5 | "encoding/pem" 6 | ) 7 | 8 | // 生成公钥 9 | func (this Curve25519) MakePublicKey() Curve25519 { 10 | this.publicKey = nil 11 | 12 | if this.privateKey == nil { 13 | err := errors.New("privateKey empty.") 14 | return this.AppendError(err) 15 | } 16 | 17 | // 导出公钥 18 | this.publicKey = &this.privateKey.PublicKey 19 | 20 | return this 21 | } 22 | 23 | // 生成密钥 der 数据 24 | func (this Curve25519) MakeKeyDer() Curve25519 { 25 | var block *pem.Block 26 | if block, _ = pem.Decode(this.keyData); block == nil { 27 | err := errors.New("keyData error.") 28 | return this.AppendError(err) 29 | } 30 | 31 | this.keyData = block.Bytes 32 | 33 | return this 34 | } 35 | -------------------------------------------------------------------------------- /cryptobin/dh/curve25519/on.go: -------------------------------------------------------------------------------- 1 | package curve25519 2 | 3 | // 引出错误信息 4 | func (this Curve25519) OnError(fn func([]error)) Curve25519 { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/dh/curve25519/to.go: -------------------------------------------------------------------------------- 1 | package curve25519 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 私钥/公钥 8 | func (this Curve25519) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // 私钥/公钥 13 | func (this Curve25519) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ================= 18 | 19 | // 输出字节 20 | func (this Curve25519) ToBytes() []byte { 21 | return this.secretData 22 | } 23 | 24 | // 输出字符 25 | func (this Curve25519) ToString() string { 26 | return string(this.secretData) 27 | } 28 | 29 | // 输出Base64 30 | func (this Curve25519) ToBase64String() string { 31 | return encoding.Base64Encode(this.secretData) 32 | } 33 | 34 | // 输出Hex 35 | func (this Curve25519) ToHexString() string { 36 | return encoding.HexEncode(this.secretData) 37 | } 38 | -------------------------------------------------------------------------------- /cryptobin/dh/curve25519/with.go: -------------------------------------------------------------------------------- 1 | package curve25519 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/pubkey/dh/curve25519" 5 | ) 6 | 7 | // 设置 PrivateKey 8 | func (this Curve25519) WithPrivateKey(data *curve25519.PrivateKey) Curve25519 { 9 | this.privateKey = data 10 | 11 | return this 12 | } 13 | 14 | // 设置 PublicKey 15 | func (this Curve25519) WithPublicKey(data *curve25519.PublicKey) Curve25519 { 16 | this.publicKey = data 17 | 18 | return this 19 | } 20 | 21 | // 设置 keyData 22 | func (this Curve25519) WithKeyData(data []byte) Curve25519 { 23 | this.keyData = data 24 | 25 | return this 26 | } 27 | 28 | // 设置 secretData 29 | func (this Curve25519) WithSecretData(data []byte) Curve25519 { 30 | this.secretData = data 31 | 32 | return this 33 | } 34 | 35 | // 设置错误 36 | func (this Curve25519) WithErrors(errs []error) Curve25519 { 37 | this.Errors = errs 38 | 39 | return this 40 | } 41 | -------------------------------------------------------------------------------- /cryptobin/dh/dh/check.go: -------------------------------------------------------------------------------- 1 | package dh 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this DH) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this.CreatePublicKey().ToKeyString() 12 | 13 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 14 | return false 15 | } 16 | 17 | if pubKeyFromPriKey == pubKeyFromPubKey { 18 | return true 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /cryptobin/dh/dh/dh.go: -------------------------------------------------------------------------------- 1 | package dh 2 | 3 | import ( 4 | cryptobin_dh "github.com/deatil/go-cryptobin/pubkey/dh/dh" 5 | ) 6 | 7 | type ( 8 | // Group 别名 9 | Group = cryptobin_dh.Group 10 | ) 11 | 12 | /** 13 | * dh 14 | * 15 | * @create 2022-8-7 16 | * @author deatil 17 | */ 18 | type DH struct { 19 | // 私钥 20 | privateKey *cryptobin_dh.PrivateKey 21 | 22 | // 公钥 23 | publicKey *cryptobin_dh.PublicKey 24 | 25 | // 分组 26 | group *cryptobin_dh.Group 27 | 28 | // [私钥/公钥]数据 29 | keyData []byte 30 | 31 | // 解析后的数据 32 | secretData []byte 33 | 34 | // 错误 35 | Errors []error 36 | } 37 | 38 | // 构造函数 39 | func NewDH() DH { 40 | group, _ := cryptobin_dh.GetMODGroup(cryptobin_dh.P2048) 41 | 42 | return DH{ 43 | group: group, 44 | Errors: make([]error, 0), 45 | } 46 | } 47 | 48 | // 构造函数 49 | func New() DH { 50 | return NewDH() 51 | } 52 | 53 | var ( 54 | // 默认 55 | defaultDH = NewDH() 56 | ) 57 | -------------------------------------------------------------------------------- /cryptobin/dh/dh/error.go: -------------------------------------------------------------------------------- 1 | package dh 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this DH) AppendError(err ...error) DH { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this DH) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/dh/dh/make.go: -------------------------------------------------------------------------------- 1 | package dh 2 | 3 | import ( 4 | "errors" 5 | "encoding/pem" 6 | ) 7 | 8 | // 生成公钥 9 | func (this DH) MakePublicKey() DH { 10 | this.publicKey = nil 11 | 12 | if this.privateKey == nil { 13 | err := errors.New("privateKey empty.") 14 | return this.AppendError(err) 15 | } 16 | 17 | // 导出公钥 18 | this.publicKey = &this.privateKey.PublicKey 19 | 20 | return this 21 | } 22 | 23 | // 生成密钥 der 数据 24 | func (this DH) MakeKeyDer() DH { 25 | var block *pem.Block 26 | if block, _ = pem.Decode(this.keyData); block == nil { 27 | err := errors.New("keyData error.") 28 | return this.AppendError(err) 29 | } 30 | 31 | this.keyData = block.Bytes 32 | 33 | return this 34 | } 35 | -------------------------------------------------------------------------------- /cryptobin/dh/dh/on.go: -------------------------------------------------------------------------------- 1 | package dh 2 | 3 | // 引出错误信息 4 | func (this DH) OnError(fn func([]error)) DH { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/dh/dh/to.go: -------------------------------------------------------------------------------- 1 | package dh 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 私钥/公钥 8 | func (this DH) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // 私钥/公钥 13 | func (this DH) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ================= 18 | 19 | // 输出字节 20 | func (this DH) ToBytes() []byte { 21 | return this.secretData 22 | } 23 | 24 | // 输出字符 25 | func (this DH) ToString() string { 26 | return string(this.secretData) 27 | } 28 | 29 | // 输出Base64 30 | func (this DH) ToBase64String() string { 31 | return encoding.Base64Encode(this.secretData) 32 | } 33 | 34 | // 输出Hex 35 | func (this DH) ToHexString() string { 36 | return encoding.HexEncode(this.secretData) 37 | } 38 | -------------------------------------------------------------------------------- /cryptobin/dh/dh/utils.go: -------------------------------------------------------------------------------- 1 | package dh 2 | 3 | import( 4 | "math/rand" 5 | ) 6 | 7 | var defaultLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") 8 | 9 | // 生成随机字符 10 | func RandomString(n int64, allowedChars ...[]rune) string { 11 | var letters []rune 12 | 13 | if len(allowedChars) == 0 { 14 | letters = defaultLetters 15 | } else { 16 | letters = allowedChars[0] 17 | } 18 | 19 | b := make([]rune, n) 20 | for i := range b { 21 | b[i] = letters[rand.Intn(len(letters))] 22 | } 23 | 24 | return string(b) 25 | } 26 | -------------------------------------------------------------------------------- /cryptobin/dh/ecdh/check.go: -------------------------------------------------------------------------------- 1 | package ecdh 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this ECDH) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this.CreatePublicKey().ToKeyString() 12 | 13 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 14 | return false 15 | } 16 | 17 | if pubKeyFromPriKey == pubKeyFromPubKey { 18 | return true 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /cryptobin/dh/ecdh/ecdh.go: -------------------------------------------------------------------------------- 1 | package ecdh 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/pubkey/dh/ecdh" 5 | ) 6 | 7 | /** 8 | * ecdh 9 | * 10 | * @create 2022-8-7 11 | * @author deatil 12 | */ 13 | type ECDH struct { 14 | // 私钥 15 | privateKey *ecdh.PrivateKey 16 | 17 | // 公钥 18 | publicKey *ecdh.PublicKey 19 | 20 | // 散列方式 21 | curve ecdh.Curve 22 | 23 | // [私钥/公钥]数据 24 | keyData []byte 25 | 26 | // 密码数据 27 | secretData []byte 28 | 29 | // 错误 30 | Errors []error 31 | } 32 | 33 | // 构造函数 34 | func NewEcdh() ECDH { 35 | curve := ecdh.P256() 36 | 37 | return ECDH{ 38 | curve: curve, 39 | Errors: make([]error, 0), 40 | } 41 | } 42 | 43 | // 构造函数 44 | func New() ECDH { 45 | return NewEcdh() 46 | } 47 | 48 | var ( 49 | // 默认 50 | defaultECDH = NewEcdh() 51 | ) 52 | -------------------------------------------------------------------------------- /cryptobin/dh/ecdh/error.go: -------------------------------------------------------------------------------- 1 | package ecdh 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this ECDH) AppendError(err ...error) ECDH { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this ECDH) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/dh/ecdh/make.go: -------------------------------------------------------------------------------- 1 | package ecdh 2 | 3 | import ( 4 | "errors" 5 | "encoding/pem" 6 | ) 7 | 8 | // 生成公钥 9 | func (this ECDH) MakePublicKey() ECDH { 10 | this.publicKey = nil 11 | 12 | if this.privateKey == nil { 13 | err := errors.New("privateKey empty.") 14 | return this.AppendError(err) 15 | } 16 | 17 | // 导出公钥 18 | this.publicKey = &this.privateKey.PublicKey 19 | 20 | return this 21 | } 22 | 23 | // 生成密钥 der 数据 24 | func (this ECDH) MakeKeyDer() ECDH { 25 | var block *pem.Block 26 | if block, _ = pem.Decode(this.keyData); block == nil { 27 | err := errors.New("keyData error.") 28 | return this.AppendError(err) 29 | } 30 | 31 | this.keyData = block.Bytes 32 | 33 | return this 34 | } 35 | -------------------------------------------------------------------------------- /cryptobin/dh/ecdh/on.go: -------------------------------------------------------------------------------- 1 | package ecdh 2 | 3 | // 引出错误信息 4 | func (this ECDH) OnError(fn func([]error)) ECDH { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/dh/ecdh/to.go: -------------------------------------------------------------------------------- 1 | package ecdh 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 私钥/公钥 8 | func (this ECDH) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // 私钥/公钥 13 | func (this ECDH) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ================= 18 | 19 | // 输出字节 20 | func (this ECDH) ToBytes() []byte { 21 | return this.secretData 22 | } 23 | 24 | // 输出字符 25 | func (this ECDH) ToString() string { 26 | return string(this.secretData) 27 | } 28 | 29 | // 输出Base64 30 | func (this ECDH) ToBase64String() string { 31 | return encoding.Base64Encode(this.secretData) 32 | } 33 | 34 | // 输出Hex 35 | func (this ECDH) ToHexString() string { 36 | return encoding.HexEncode(this.secretData) 37 | } 38 | -------------------------------------------------------------------------------- /cryptobin/dsa/check.go: -------------------------------------------------------------------------------- 1 | package dsa 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this DSA) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this.CreatePublicKey().ToKeyString() 12 | 13 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 14 | return false 15 | } 16 | 17 | if pubKeyFromPriKey == pubKeyFromPubKey { 18 | return true 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /cryptobin/dsa/error.go: -------------------------------------------------------------------------------- 1 | package dsa 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this DSA) AppendError(err ...error) DSA { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this DSA) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/dsa/get.go: -------------------------------------------------------------------------------- 1 | package dsa 2 | 3 | import ( 4 | "crypto/dsa" 5 | ) 6 | 7 | // 获取 PrivateKey 8 | func (this DSA) GetPrivateKey() *dsa.PrivateKey { 9 | return this.privateKey 10 | } 11 | 12 | // 获取 PublicKey 13 | func (this DSA) GetPublicKey() *dsa.PublicKey { 14 | return this.publicKey 15 | } 16 | 17 | // 获取 keyData 18 | func (this DSA) GetKeyData() []byte { 19 | return this.keyData 20 | } 21 | 22 | // 获取 data 23 | func (this DSA) GetData() []byte { 24 | return this.data 25 | } 26 | 27 | // 获取 parsedData 28 | func (this DSA) GetParsedData() []byte { 29 | return this.parsedData 30 | } 31 | 32 | // 获取 hash 类型 33 | func (this DSA) GetSignHash() HashFunc { 34 | return this.signHash 35 | } 36 | 37 | // get Encoding type 38 | func (this DSA) GetEncoding() EncodingType { 39 | return this.encoding 40 | } 41 | 42 | // 获取验证后情况 43 | func (this DSA) GetVerify() bool { 44 | return this.verify 45 | } 46 | 47 | // 获取错误 48 | func (this DSA) GetErrors() []error { 49 | return this.Errors 50 | } 51 | -------------------------------------------------------------------------------- /cryptobin/dsa/make.go: -------------------------------------------------------------------------------- 1 | package dsa 2 | 3 | import ( 4 | "errors" 5 | "encoding/pem" 6 | ) 7 | 8 | // 生成公钥 9 | func (this DSA) MakePublicKey() DSA { 10 | this.publicKey = nil 11 | 12 | if this.privateKey == nil { 13 | err := errors.New("privateKey empty.") 14 | return this.AppendError(err) 15 | } 16 | 17 | // 导出公钥 18 | this.publicKey = &this.privateKey.PublicKey 19 | 20 | return this 21 | } 22 | 23 | // 生成密钥 der 数据 24 | func (this DSA) MakeKeyDer() DSA { 25 | var block *pem.Block 26 | if block, _ = pem.Decode(this.keyData); block == nil { 27 | err := errors.New("keyData error.") 28 | return this.AppendError(err) 29 | } 30 | 31 | this.keyData = block.Bytes 32 | 33 | return this 34 | } 35 | -------------------------------------------------------------------------------- /cryptobin/dsa/on.go: -------------------------------------------------------------------------------- 1 | package dsa 2 | 3 | // 引出错误信息 4 | func (this DSA) OnError(fn func([]error)) DSA { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/dsa/to.go: -------------------------------------------------------------------------------- 1 | package dsa 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 私钥/公钥 8 | func (this DSA) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // 私钥/公钥 13 | func (this DSA) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ========== 18 | 19 | // 输出字节 20 | func (this DSA) ToBytes() []byte { 21 | return this.parsedData 22 | } 23 | 24 | // 输出字符 25 | func (this DSA) ToString() string { 26 | return string(this.parsedData) 27 | } 28 | 29 | // 输出Base64 30 | func (this DSA) ToBase64String() string { 31 | return encoding.Base64Encode(this.parsedData) 32 | } 33 | 34 | // 输出Hex 35 | func (this DSA) ToHexString() string { 36 | return encoding.HexEncode(this.parsedData) 37 | } 38 | 39 | // ========== 40 | 41 | // 验证结果 42 | func (this DSA) ToVerify() bool { 43 | return this.verify 44 | } 45 | 46 | // 验证结果,返回 int 类型 47 | func (this DSA) ToVerifyInt() int { 48 | if this.verify { 49 | return 1 50 | } 51 | 52 | return 0 53 | } 54 | -------------------------------------------------------------------------------- /cryptobin/ecdh/check.go: -------------------------------------------------------------------------------- 1 | package ecdh 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this ECDH) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this.CreatePublicKey().ToKeyString() 12 | 13 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 14 | return false 15 | } 16 | 17 | if pubKeyFromPriKey == pubKeyFromPubKey { 18 | return true 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /cryptobin/ecdh/ecdh.go: -------------------------------------------------------------------------------- 1 | package ecdh 2 | 3 | import ( 4 | "crypto/ecdh" 5 | ) 6 | 7 | /** 8 | * ecdh 9 | * go 最低版本需要 `1.20rc1`。 10 | * 11 | * @create 2022-8-7 12 | * @author deatil 13 | */ 14 | type ECDH struct { 15 | // 私钥 16 | privateKey *ecdh.PrivateKey 17 | 18 | // 公钥 19 | publicKey *ecdh.PublicKey 20 | 21 | // 散列方式 22 | curve ecdh.Curve 23 | 24 | // [私钥/公钥]数据 25 | keyData []byte 26 | 27 | // 密码数据 28 | secretData []byte 29 | 30 | // 错误 31 | Errors []error 32 | } 33 | 34 | // 构造函数 35 | func NewECDH() ECDH { 36 | return ECDH{ 37 | curve: ecdh.P256(), 38 | Errors: make([]error, 0), 39 | } 40 | } 41 | 42 | // 构造函数 43 | func New() ECDH { 44 | return NewECDH() 45 | } 46 | 47 | var ( 48 | // 默认 49 | defaultECDH = NewECDH() 50 | ) 51 | -------------------------------------------------------------------------------- /cryptobin/ecdh/error.go: -------------------------------------------------------------------------------- 1 | package ecdh 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this ECDH) AppendError(err ...error) ECDH { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this ECDH) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/ecdh/get.go: -------------------------------------------------------------------------------- 1 | package ecdh 2 | 3 | import ( 4 | "crypto/ecdh" 5 | ) 6 | 7 | // 获取 PrivateKey 8 | func (this ECDH) GetPrivateKey() *ecdh.PrivateKey { 9 | return this.privateKey 10 | } 11 | 12 | // 获取 PublicKey 13 | func (this ECDH) GetPublicKey() *ecdh.PublicKey { 14 | return this.publicKey 15 | } 16 | 17 | // 获取散列方式 18 | func (this ECDH) GetCurve() ecdh.Curve { 19 | return this.curve 20 | } 21 | 22 | // 获取 keyData 23 | func (this ECDH) GetKeyData() []byte { 24 | return this.keyData 25 | } 26 | 27 | // 获取 secretData 28 | func (this ECDH) GetSecretData() []byte { 29 | return this.secretData 30 | } 31 | 32 | // 获取错误 33 | func (this ECDH) GetErrors() []error { 34 | return this.Errors 35 | } 36 | -------------------------------------------------------------------------------- /cryptobin/ecdh/make.go: -------------------------------------------------------------------------------- 1 | package ecdh 2 | 3 | import ( 4 | "errors" 5 | "encoding/pem" 6 | ) 7 | 8 | // 生成公钥 9 | func (this ECDH) MakePublicKey() ECDH { 10 | this.publicKey = nil 11 | 12 | if this.privateKey == nil { 13 | err := errors.New("privateKey empty.") 14 | return this.AppendError(err) 15 | } 16 | 17 | // 导出公钥 18 | this.publicKey = this.privateKey.PublicKey() 19 | 20 | return this 21 | } 22 | 23 | // 生成密钥 der 数据 24 | func (this ECDH) MakeKeyDer() ECDH { 25 | var block *pem.Block 26 | if block, _ = pem.Decode(this.keyData); block == nil { 27 | err := errors.New("keyData error.") 28 | return this.AppendError(err) 29 | } 30 | 31 | this.keyData = block.Bytes 32 | 33 | return this 34 | } 35 | -------------------------------------------------------------------------------- /cryptobin/ecdh/on.go: -------------------------------------------------------------------------------- 1 | package ecdh 2 | 3 | // 引出错误信息 4 | func (this ECDH) OnError(fn func([]error)) ECDH { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/ecdh/to.go: -------------------------------------------------------------------------------- 1 | package ecdh 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 私钥/公钥 8 | func (this ECDH) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // 私钥/公钥 13 | func (this ECDH) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ================= 18 | 19 | // 输出字节 20 | func (this ECDH) ToBytes() []byte { 21 | return this.secretData 22 | } 23 | 24 | // 输出字符 25 | func (this ECDH) ToString() string { 26 | return string(this.secretData) 27 | } 28 | 29 | // 输出Base64 30 | func (this ECDH) ToBase64String() string { 31 | return encoding.Base64Encode(this.secretData) 32 | } 33 | 34 | // 输出Hex 35 | func (this ECDH) ToHexString() string { 36 | return encoding.HexEncode(this.secretData) 37 | } 38 | -------------------------------------------------------------------------------- /cryptobin/ecdsa/check.go: -------------------------------------------------------------------------------- 1 | package ecdsa 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this ECDSA) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this.CreatePublicKey().ToKeyString() 12 | 13 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 14 | return false 15 | } 16 | 17 | if pubKeyFromPriKey == pubKeyFromPubKey { 18 | return true 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /cryptobin/ecdsa/error.go: -------------------------------------------------------------------------------- 1 | package ecdsa 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this ECDSA) AppendError(err ...error) ECDSA { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this ECDSA) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/ecdsa/make.go: -------------------------------------------------------------------------------- 1 | package ecdsa 2 | 3 | import( 4 | "errors" 5 | "encoding/pem" 6 | ) 7 | 8 | // 生成公钥 9 | func (this ECDSA) MakePublicKey() ECDSA { 10 | this.publicKey = nil 11 | 12 | if this.privateKey == nil { 13 | err := errors.New("privateKey empty.") 14 | return this.AppendError(err) 15 | } 16 | 17 | this.publicKey = &this.privateKey.PublicKey 18 | 19 | return this 20 | } 21 | 22 | // 生成密钥 der 数据 23 | func (this ECDSA) MakeKeyDer() ECDSA { 24 | var block *pem.Block 25 | if block, _ = pem.Decode(this.keyData); block == nil { 26 | err := errors.New("keyData error.") 27 | return this.AppendError(err) 28 | } 29 | 30 | this.keyData = block.Bytes 31 | 32 | return this 33 | } 34 | -------------------------------------------------------------------------------- /cryptobin/ecdsa/on.go: -------------------------------------------------------------------------------- 1 | package ecdsa 2 | 3 | // 引出错误信息 4 | func (this ECDSA) OnError(fn func([]error)) ECDSA { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/ecdsa/to.go: -------------------------------------------------------------------------------- 1 | package ecdsa 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 私钥/公钥 8 | func (this ECDSA) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // 私钥/公钥 13 | func (this ECDSA) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ========== 18 | 19 | // 输出字节 20 | func (this ECDSA) ToBytes() []byte { 21 | return this.parsedData 22 | } 23 | 24 | // 输出字符 25 | func (this ECDSA) ToString() string { 26 | return string(this.parsedData) 27 | } 28 | 29 | // 输出Base64 30 | func (this ECDSA) ToBase64String() string { 31 | return encoding.Base64Encode(this.parsedData) 32 | } 33 | 34 | // 输出Hex 35 | func (this ECDSA) ToHexString() string { 36 | return encoding.HexEncode(this.parsedData) 37 | } 38 | 39 | // ========== 40 | 41 | // 验证结果 42 | func (this ECDSA) ToVerify() bool { 43 | return this.verify 44 | } 45 | 46 | // 验证结果,返回 int 类型 47 | func (this ECDSA) ToVerifyInt() int { 48 | if this.verify { 49 | return 1 50 | } 51 | 52 | return 0 53 | } 54 | -------------------------------------------------------------------------------- /cryptobin/ecgdsa/check.go: -------------------------------------------------------------------------------- 1 | package ecgdsa 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this ECGDSA) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this.CreatePublicKey().ToKeyString() 12 | 13 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 14 | return false 15 | } 16 | 17 | if pubKeyFromPriKey == pubKeyFromPubKey { 18 | return true 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /cryptobin/ecgdsa/error.go: -------------------------------------------------------------------------------- 1 | package ecgdsa 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this ECGDSA) AppendError(err ...error) ECGDSA { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this ECGDSA) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/ecgdsa/make.go: -------------------------------------------------------------------------------- 1 | package ecgdsa 2 | 3 | import( 4 | "errors" 5 | "encoding/pem" 6 | ) 7 | 8 | // 生成公钥 9 | func (this ECGDSA) MakePublicKey() ECGDSA { 10 | this.publicKey = nil 11 | 12 | if this.privateKey == nil { 13 | err := errors.New("privateKey empty.") 14 | return this.AppendError(err) 15 | } 16 | 17 | this.publicKey = &this.privateKey.PublicKey 18 | 19 | return this 20 | } 21 | 22 | // 生成密钥 der 数据 23 | func (this ECGDSA) MakeKeyDer() ECGDSA { 24 | var block *pem.Block 25 | if block, _ = pem.Decode(this.keyData); block == nil { 26 | err := errors.New("keyData error.") 27 | return this.AppendError(err) 28 | } 29 | 30 | this.keyData = block.Bytes 31 | 32 | return this 33 | } 34 | -------------------------------------------------------------------------------- /cryptobin/ecgdsa/on.go: -------------------------------------------------------------------------------- 1 | package ecgdsa 2 | 3 | // 引出错误信息 4 | func (this ECGDSA) OnError(fn func([]error)) ECGDSA { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/ecgdsa/to.go: -------------------------------------------------------------------------------- 1 | package ecgdsa 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 私钥/公钥 8 | func (this ECGDSA) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // 私钥/公钥 13 | func (this ECGDSA) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ========== 18 | 19 | // 输出字节 20 | func (this ECGDSA) ToBytes() []byte { 21 | return this.parsedData 22 | } 23 | 24 | // 输出字符 25 | func (this ECGDSA) ToString() string { 26 | return string(this.parsedData) 27 | } 28 | 29 | // 输出Base64 30 | func (this ECGDSA) ToBase64String() string { 31 | return encoding.Base64Encode(this.parsedData) 32 | } 33 | 34 | // 输出Hex 35 | func (this ECGDSA) ToHexString() string { 36 | return encoding.HexEncode(this.parsedData) 37 | } 38 | 39 | // ========== 40 | 41 | // 验证结果 42 | func (this ECGDSA) ToVerify() bool { 43 | return this.verify 44 | } 45 | 46 | // 验证结果,返回 int 类型 47 | func (this ECGDSA) ToVerifyInt() int { 48 | if this.verify { 49 | return 1 50 | } 51 | 52 | return 0 53 | } 54 | -------------------------------------------------------------------------------- /cryptobin/ecsdsa/check.go: -------------------------------------------------------------------------------- 1 | package ecsdsa 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this ECSDSA) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this.CreatePublicKey().ToKeyString() 12 | 13 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 14 | return false 15 | } 16 | 17 | if pubKeyFromPriKey == pubKeyFromPubKey { 18 | return true 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /cryptobin/ecsdsa/error.go: -------------------------------------------------------------------------------- 1 | package ecsdsa 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this ECSDSA) AppendError(err ...error) ECSDSA { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this ECSDSA) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/ecsdsa/make.go: -------------------------------------------------------------------------------- 1 | package ecsdsa 2 | 3 | import( 4 | "errors" 5 | "encoding/pem" 6 | ) 7 | 8 | // 生成公钥 9 | func (this ECSDSA) MakePublicKey() ECSDSA { 10 | this.publicKey = nil 11 | 12 | if this.privateKey == nil { 13 | err := errors.New("privateKey empty.") 14 | return this.AppendError(err) 15 | } 16 | 17 | this.publicKey = &this.privateKey.PublicKey 18 | 19 | return this 20 | } 21 | 22 | // 生成密钥 der 数据 23 | func (this ECSDSA) MakeKeyDer() ECSDSA { 24 | var block *pem.Block 25 | if block, _ = pem.Decode(this.keyData); block == nil { 26 | err := errors.New("keyData error.") 27 | return this.AppendError(err) 28 | } 29 | 30 | this.keyData = block.Bytes 31 | 32 | return this 33 | } 34 | -------------------------------------------------------------------------------- /cryptobin/ecsdsa/on.go: -------------------------------------------------------------------------------- 1 | package ecsdsa 2 | 3 | // 引出错误信息 4 | func (this ECSDSA) OnError(fn func([]error)) ECSDSA { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/ecsdsa/to.go: -------------------------------------------------------------------------------- 1 | package ecsdsa 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 私钥/公钥 8 | func (this ECSDSA) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // 私钥/公钥 13 | func (this ECSDSA) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ========== 18 | 19 | // 输出字节 20 | func (this ECSDSA) ToBytes() []byte { 21 | return this.parsedData 22 | } 23 | 24 | // 输出字符 25 | func (this ECSDSA) ToString() string { 26 | return string(this.parsedData) 27 | } 28 | 29 | // 输出Base64 30 | func (this ECSDSA) ToBase64String() string { 31 | return encoding.Base64Encode(this.parsedData) 32 | } 33 | 34 | // 输出Hex 35 | func (this ECSDSA) ToHexString() string { 36 | return encoding.HexEncode(this.parsedData) 37 | } 38 | 39 | // ========== 40 | 41 | // 验证结果 42 | func (this ECSDSA) ToVerify() bool { 43 | return this.verify 44 | } 45 | 46 | // 验证结果,返回 int 类型 47 | func (this ECSDSA) ToVerifyInt() int { 48 | if this.verify { 49 | return 1 50 | } 51 | 52 | return 0 53 | } 54 | -------------------------------------------------------------------------------- /cryptobin/ed448/check.go: -------------------------------------------------------------------------------- 1 | package ed448 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this ED448) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this. 12 | CreatePublicKey(). 13 | ToKeyString() 14 | 15 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 16 | return false 17 | } 18 | 19 | if pubKeyFromPriKey == pubKeyFromPubKey { 20 | return true 21 | } 22 | 23 | return false 24 | } 25 | -------------------------------------------------------------------------------- /cryptobin/ed448/error.go: -------------------------------------------------------------------------------- 1 | package ed448 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this ED448) AppendError(err ...error) ED448 { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this ED448) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/ed448/make.go: -------------------------------------------------------------------------------- 1 | package ed448 2 | 3 | import ( 4 | "errors" 5 | "encoding/pem" 6 | 7 | "github.com/deatil/go-cryptobin/pubkey/ed448" 8 | ) 9 | 10 | // 生成公钥 11 | func (this ED448) MakePublicKey() ED448 { 12 | this.publicKey = ed448.PublicKey{} 13 | 14 | if this.privateKey == nil { 15 | err := errors.New("privateKey empty.") 16 | return this.AppendError(err) 17 | } 18 | 19 | // 导出公钥 20 | this.publicKey = this.privateKey.Public().(ed448.PublicKey) 21 | 22 | return this 23 | } 24 | 25 | // 生成密钥 der 数据 26 | func (this ED448) MakeKeyDer() ED448 { 27 | var block *pem.Block 28 | if block, _ = pem.Decode(this.keyData); block == nil { 29 | err := errors.New("keyData error.") 30 | return this.AppendError(err) 31 | } 32 | 33 | this.keyData = block.Bytes 34 | 35 | return this 36 | } 37 | -------------------------------------------------------------------------------- /cryptobin/ed448/on.go: -------------------------------------------------------------------------------- 1 | package ed448 2 | 3 | // 引出错误信息 4 | func (this ED448) OnError(fn func([]error)) ED448 { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/ed448/sign.go: -------------------------------------------------------------------------------- 1 | package ed448 2 | 3 | import ( 4 | "errors" 5 | "crypto/rand" 6 | 7 | "github.com/deatil/go-cryptobin/pubkey/ed448" 8 | ) 9 | 10 | // 私钥签名 11 | func (this ED448) Sign() ED448 { 12 | if this.privateKey == nil { 13 | err := errors.New("privateKey empty.") 14 | return this.AppendError(err) 15 | } 16 | 17 | sig, err := this.privateKey.Sign(rand.Reader, this.data, this.options) 18 | if err != nil { 19 | return this.AppendError(err) 20 | } 21 | 22 | this.parsedData = []byte(sig) 23 | 24 | return this 25 | } 26 | 27 | // 公钥验证 28 | func (this ED448) Verify(data []byte) ED448 { 29 | if this.publicKey == nil { 30 | err := errors.New("publicKey empty.") 31 | return this.AppendError(err) 32 | } 33 | 34 | err := ed448.VerifyWithOptions(this.publicKey, data, this.data, this.options) 35 | if err != nil { 36 | return this.AppendError(err) 37 | } 38 | 39 | this.verify = true 40 | 41 | return this 42 | } 43 | -------------------------------------------------------------------------------- /cryptobin/ed448/to.go: -------------------------------------------------------------------------------- 1 | package ed448 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 私钥/公钥 8 | func (this ED448) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // 私钥/公钥 13 | func (this ED448) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ========== 18 | 19 | // 输出字节 20 | func (this ED448) ToBytes() []byte { 21 | return this.parsedData 22 | } 23 | 24 | // 输出字符 25 | func (this ED448) ToString() string { 26 | return string(this.parsedData) 27 | } 28 | 29 | // 输出Base64 30 | func (this ED448) ToBase64String() string { 31 | return encoding.Base64Encode(this.parsedData) 32 | } 33 | 34 | // 输出Hex 35 | func (this ED448) ToHexString() string { 36 | return encoding.HexEncode(this.parsedData) 37 | } 38 | 39 | // ========== 40 | 41 | // 验证结果 42 | func (this ED448) ToVerify() bool { 43 | return this.verify 44 | } 45 | 46 | // 验证结果,返回 int 类型 47 | func (this ED448) ToVerifyInt() int { 48 | if this.verify { 49 | return 1 50 | } 51 | 52 | return 0 53 | } 54 | -------------------------------------------------------------------------------- /cryptobin/eddsa/check.go: -------------------------------------------------------------------------------- 1 | package eddsa 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this EdDSA) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this. 12 | CreatePublicKey(). 13 | ToKeyString() 14 | 15 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 16 | return false 17 | } 18 | 19 | if pubKeyFromPriKey == pubKeyFromPubKey { 20 | return true 21 | } 22 | 23 | return false 24 | } 25 | -------------------------------------------------------------------------------- /cryptobin/eddsa/eddsa.go: -------------------------------------------------------------------------------- 1 | package eddsa 2 | 3 | import ( 4 | "crypto" 5 | "crypto/ed25519" 6 | ) 7 | 8 | type ( 9 | // 设置 10 | Options = ed25519.Options 11 | ) 12 | 13 | /** 14 | * EdDSA 15 | * 16 | * @create 2022-4-3 17 | * @author deatil 18 | */ 19 | type EdDSA struct { 20 | // 私钥 21 | privateKey ed25519.PrivateKey 22 | 23 | // 公钥 24 | publicKey ed25519.PublicKey 25 | 26 | // 设置 27 | options *Options 28 | 29 | // [私钥/公钥]数据 30 | keyData []byte 31 | 32 | // 传入数据 33 | data []byte 34 | 35 | // 解析后的数据 36 | parsedData []byte 37 | 38 | // 验证结果 39 | verify bool 40 | 41 | // 错误 42 | Errors []error 43 | } 44 | 45 | // 构造函数 46 | func NewEdDSA() EdDSA { 47 | return EdDSA{ 48 | options: &Options{ 49 | Hash: crypto.Hash(0), 50 | Context: "", 51 | }, 52 | verify: false, 53 | Errors: make([]error, 0), 54 | } 55 | } 56 | 57 | // 构造函数 58 | func New() EdDSA { 59 | return NewEdDSA() 60 | } 61 | 62 | var ( 63 | // 默认 64 | defaultEdDSA = NewEdDSA() 65 | ) 66 | -------------------------------------------------------------------------------- /cryptobin/eddsa/error.go: -------------------------------------------------------------------------------- 1 | package eddsa 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this EdDSA) AppendError(err ...error) EdDSA { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this EdDSA) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/eddsa/make.go: -------------------------------------------------------------------------------- 1 | package eddsa 2 | 3 | import ( 4 | "errors" 5 | "crypto/ed25519" 6 | "encoding/pem" 7 | ) 8 | 9 | // 生成公钥 10 | func (this EdDSA) MakePublicKey() EdDSA { 11 | this.publicKey = ed25519.PublicKey{} 12 | 13 | if this.privateKey == nil { 14 | err := errors.New("privateKey empty.") 15 | return this.AppendError(err) 16 | } 17 | 18 | // 导出公钥 19 | this.publicKey = this.privateKey.Public().(ed25519.PublicKey) 20 | 21 | return this 22 | } 23 | 24 | // 生成密钥 der 数据 25 | func (this EdDSA) MakeKeyDer() EdDSA { 26 | var block *pem.Block 27 | if block, _ = pem.Decode(this.keyData); block == nil { 28 | err := errors.New("keyData error.") 29 | return this.AppendError(err) 30 | } 31 | 32 | this.keyData = block.Bytes 33 | 34 | return this 35 | } 36 | -------------------------------------------------------------------------------- /cryptobin/eddsa/on.go: -------------------------------------------------------------------------------- 1 | package eddsa 2 | 3 | // 引出错误信息 4 | func (this EdDSA) OnError(fn func([]error)) EdDSA { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/eddsa/to.go: -------------------------------------------------------------------------------- 1 | package eddsa 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 私钥/公钥 8 | func (this EdDSA) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // 私钥/公钥 13 | func (this EdDSA) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ========== 18 | 19 | // 输出字节 20 | func (this EdDSA) ToBytes() []byte { 21 | return this.parsedData 22 | } 23 | 24 | // 输出字符 25 | func (this EdDSA) ToString() string { 26 | return string(this.parsedData) 27 | } 28 | 29 | // 输出Base64 30 | func (this EdDSA) ToBase64String() string { 31 | return encoding.Base64Encode(this.parsedData) 32 | } 33 | 34 | // 输出Hex 35 | func (this EdDSA) ToHexString() string { 36 | return encoding.HexEncode(this.parsedData) 37 | } 38 | 39 | // ========== 40 | 41 | // 验证结果 42 | func (this EdDSA) ToVerify() bool { 43 | return this.verify 44 | } 45 | 46 | // 验证结果,返回 int 类型 47 | func (this EdDSA) ToVerifyInt() int { 48 | if this.verify { 49 | return 1 50 | } 51 | 52 | return 0 53 | } 54 | -------------------------------------------------------------------------------- /cryptobin/elgamal/check.go: -------------------------------------------------------------------------------- 1 | package elgamal 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this ElGamal) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this.CreatePublicKey().ToKeyString() 12 | 13 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 14 | return false 15 | } 16 | 17 | if pubKeyFromPriKey == pubKeyFromPubKey { 18 | return true 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /cryptobin/elgamal/error.go: -------------------------------------------------------------------------------- 1 | package elgamal 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this ElGamal) AppendError(err ...error) ElGamal { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this ElGamal) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/elgamal/get.go: -------------------------------------------------------------------------------- 1 | package elgamal 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/pubkey/elgamal" 5 | ) 6 | 7 | // 获取 PrivateKey 8 | func (this ElGamal) GetPrivateKey() *elgamal.PrivateKey { 9 | return this.privateKey 10 | } 11 | 12 | // 获取 PublicKey 13 | func (this ElGamal) GetPublicKey() *elgamal.PublicKey { 14 | return this.publicKey 15 | } 16 | 17 | // 获取 hash 类型 18 | func (this ElGamal) GetSignHash() HashFunc { 19 | return this.signHash 20 | } 21 | 22 | // 获取 keyData 23 | func (this ElGamal) GetKeyData() []byte { 24 | return this.keyData 25 | } 26 | 27 | // 获取 data 28 | func (this ElGamal) GetData() []byte { 29 | return this.data 30 | } 31 | 32 | // 获取 parsedData 33 | func (this ElGamal) GetParsedData() []byte { 34 | return this.parsedData 35 | } 36 | 37 | // get Encoding type 38 | func (this ElGamal) GetEncoding() EncodingType { 39 | return this.encoding 40 | } 41 | 42 | // 获取验证后情况 43 | func (this ElGamal) GetVerify() bool { 44 | return this.verify 45 | } 46 | 47 | // 获取错误 48 | func (this ElGamal) GetErrors() []error { 49 | return this.Errors 50 | } 51 | -------------------------------------------------------------------------------- /cryptobin/elgamal/make.go: -------------------------------------------------------------------------------- 1 | package elgamal 2 | 3 | import ( 4 | "errors" 5 | "encoding/pem" 6 | ) 7 | 8 | // 生成公钥 9 | func (this ElGamal) MakePublicKey() ElGamal { 10 | this.publicKey = nil 11 | 12 | if this.privateKey == nil { 13 | err := errors.New("privateKey empty.") 14 | return this.AppendError(err) 15 | } 16 | 17 | // 导出公钥 18 | this.publicKey = &this.privateKey.PublicKey 19 | 20 | return this 21 | } 22 | 23 | // 生成密钥 der 数据 24 | func (this ElGamal) MakeKeyDer() ElGamal { 25 | var block *pem.Block 26 | if block, _ = pem.Decode(this.keyData); block == nil { 27 | err := errors.New("keyData error.") 28 | return this.AppendError(err) 29 | } 30 | 31 | this.keyData = block.Bytes 32 | 33 | return this 34 | } 35 | -------------------------------------------------------------------------------- /cryptobin/elgamal/on.go: -------------------------------------------------------------------------------- 1 | package elgamal 2 | 3 | // 引出错误信息 4 | func (this ElGamal) OnError(fn func([]error)) ElGamal { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/elgamal/to.go: -------------------------------------------------------------------------------- 1 | package elgamal 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 私钥/公钥 8 | func (this ElGamal) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // 私钥/公钥 13 | func (this ElGamal) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ========== 18 | 19 | // 输出字节 20 | func (this ElGamal) ToBytes() []byte { 21 | return this.parsedData 22 | } 23 | 24 | // 输出字符 25 | func (this ElGamal) ToString() string { 26 | return string(this.parsedData) 27 | } 28 | 29 | // 输出Base64 30 | func (this ElGamal) ToBase64String() string { 31 | return encoding.Base64Encode(this.parsedData) 32 | } 33 | 34 | // 输出Hex 35 | func (this ElGamal) ToHexString() string { 36 | return encoding.HexEncode(this.parsedData) 37 | } 38 | 39 | // ========== 40 | 41 | // 验证结果 42 | func (this ElGamal) ToVerify() bool { 43 | return this.verify 44 | } 45 | 46 | // 验证结果,返回 int 类型 47 | func (this ElGamal) ToVerifyInt() int { 48 | if this.verify { 49 | return 1 50 | } 51 | 52 | return 0 53 | } 54 | -------------------------------------------------------------------------------- /cryptobin/gost/check.go: -------------------------------------------------------------------------------- 1 | package gost 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this Gost) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this.CreatePublicKey().ToKeyString() 12 | 13 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 14 | return false 15 | } 16 | 17 | if pubKeyFromPriKey == pubKeyFromPubKey { 18 | return true 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /cryptobin/gost/error.go: -------------------------------------------------------------------------------- 1 | package gost 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this Gost) AppendError(err ...error) Gost { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this Gost) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/gost/make.go: -------------------------------------------------------------------------------- 1 | package gost 2 | 3 | import ( 4 | "errors" 5 | "encoding/pem" 6 | ) 7 | 8 | // 生成公钥 9 | func (this Gost) MakePublicKey() Gost { 10 | this.publicKey = nil 11 | 12 | if this.privateKey == nil { 13 | err := errors.New("privateKey empty.") 14 | return this.AppendError(err) 15 | } 16 | 17 | // 导出公钥 18 | this.publicKey = &this.privateKey.PublicKey 19 | 20 | return this 21 | } 22 | 23 | // 生成密钥 der 数据 24 | func (this Gost) MakeKeyDer() Gost { 25 | var block *pem.Block 26 | if block, _ = pem.Decode(this.keyData); block == nil { 27 | err := errors.New("keyData error.") 28 | return this.AppendError(err) 29 | } 30 | 31 | this.keyData = block.Bytes 32 | 33 | return this 34 | } 35 | -------------------------------------------------------------------------------- /cryptobin/gost/on.go: -------------------------------------------------------------------------------- 1 | package gost 2 | 3 | // 引出错误信息 4 | func (this Gost) OnError(fn func([]error)) Gost { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/rsa/check.go: -------------------------------------------------------------------------------- 1 | package rsa 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this RSA) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePKCS8PublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this.CreatePKCS8PublicKey().ToKeyString() 12 | 13 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 14 | return false 15 | } 16 | 17 | if pubKeyFromPriKey == pubKeyFromPubKey { 18 | return true 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /cryptobin/rsa/error.go: -------------------------------------------------------------------------------- 1 | package rsa 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this RSA) AppendError(err ...error) RSA { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this RSA) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/rsa/make.go: -------------------------------------------------------------------------------- 1 | package rsa 2 | 3 | import( 4 | "errors" 5 | "encoding/pem" 6 | ) 7 | 8 | // 生成公钥 9 | func (this RSA) MakePublicKey() RSA { 10 | this.publicKey = nil 11 | 12 | if this.privateKey == nil { 13 | err := errors.New("privateKey empty.") 14 | return this.AppendError(err) 15 | } 16 | 17 | this.publicKey = &this.privateKey.PublicKey 18 | 19 | return this 20 | } 21 | 22 | // 生成密钥 der 数据 23 | func (this RSA) MakeKeyDer() RSA { 24 | var block *pem.Block 25 | if block, _ = pem.Decode(this.keyData); block == nil { 26 | err := errors.New("keyData error.") 27 | return this.AppendError(err) 28 | } 29 | 30 | this.keyData = block.Bytes 31 | 32 | return this 33 | } 34 | -------------------------------------------------------------------------------- /cryptobin/rsa/on.go: -------------------------------------------------------------------------------- 1 | package rsa 2 | 3 | // 引出错误信息 4 | func (this RSA) OnError(fn func([]error)) RSA { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/rsa/rsa.go: -------------------------------------------------------------------------------- 1 | package rsa 2 | 3 | import ( 4 | "hash" 5 | "crypto" 6 | "crypto/rsa" 7 | "crypto/sha1" 8 | ) 9 | 10 | /** 11 | * RSA 加密 12 | * 13 | * @create 2021-8-28 14 | * @author deatil 15 | */ 16 | type RSA struct { 17 | // 私钥 18 | privateKey *rsa.PrivateKey 19 | 20 | // 公钥 21 | publicKey *rsa.PublicKey 22 | 23 | // 签名验证类型 24 | signHash crypto.Hash 25 | 26 | // EncryptOAEP hash.Hash 27 | oaepHash hash.Hash 28 | 29 | // EncryptOAEP label 30 | oaepLabel []byte 31 | 32 | // [私钥/公钥]数据 33 | keyData []byte 34 | 35 | // 传入数据 36 | data []byte 37 | 38 | // 解析后的数据 39 | parsedData []byte 40 | 41 | // 验证结果 42 | verify bool 43 | 44 | // 错误 45 | Errors []error 46 | } 47 | 48 | // 构造函数 49 | func NewRSA() RSA { 50 | return RSA{ 51 | signHash: crypto.SHA256, 52 | oaepHash: sha1.New(), 53 | verify: false, 54 | Errors: make([]error, 0), 55 | } 56 | } 57 | 58 | // 构造函数 59 | func New() RSA { 60 | return NewRSA() 61 | } 62 | 63 | var ( 64 | // 默认 65 | defaultRSA = NewRSA() 66 | ) 67 | -------------------------------------------------------------------------------- /cryptobin/rsa/to.go: -------------------------------------------------------------------------------- 1 | package rsa 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 私钥/公钥 8 | func (this RSA) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // 私钥/公钥 13 | func (this RSA) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ========== 18 | 19 | // 输出字节 20 | func (this RSA) ToBytes() []byte { 21 | return this.parsedData 22 | } 23 | 24 | // 输出字符 25 | func (this RSA) ToString() string { 26 | return string(this.parsedData) 27 | } 28 | 29 | // 输出Base64 30 | func (this RSA) ToBase64String() string { 31 | return encoding.Base64Encode(this.parsedData) 32 | } 33 | 34 | // 输出Hex 35 | func (this RSA) ToHexString() string { 36 | return encoding.HexEncode(this.parsedData) 37 | } 38 | 39 | // ========== 40 | 41 | // 验证结果 42 | func (this RSA) ToVerify() bool { 43 | return this.verify 44 | } 45 | 46 | // 验证结果,返回 int 类型 47 | func (this RSA) ToVerifyInt() int { 48 | if this.verify { 49 | return 1 50 | } 51 | 52 | return 0 53 | } 54 | -------------------------------------------------------------------------------- /cryptobin/sm2/check.go: -------------------------------------------------------------------------------- 1 | package sm2 2 | 3 | // 检测公钥私钥是否匹配 4 | func (this SM2) CheckKeyPair() bool { 5 | // 私钥导出的公钥 6 | pubKeyFromPriKey := this.MakePublicKey(). 7 | CreatePublicKey(). 8 | ToKeyString() 9 | 10 | // 公钥数据 11 | pubKeyFromPubKey := this.CreatePublicKey().ToKeyString() 12 | 13 | if pubKeyFromPriKey == "" || pubKeyFromPubKey == "" { 14 | return false 15 | } 16 | 17 | if pubKeyFromPriKey == pubKeyFromPubKey { 18 | return true 19 | } 20 | 21 | return false 22 | } 23 | -------------------------------------------------------------------------------- /cryptobin/sm2/error.go: -------------------------------------------------------------------------------- 1 | package sm2 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // 添加错误 8 | func (this SM2) AppendError(errs ...error) SM2 { 9 | this.Errors = append(this.Errors, errs...) 10 | 11 | return this 12 | } 13 | 14 | // 获取错误 15 | func (this SM2) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/sm2/make.go: -------------------------------------------------------------------------------- 1 | package sm2 2 | 3 | import( 4 | "errors" 5 | "encoding/pem" 6 | ) 7 | 8 | // 生成公钥 9 | func (this SM2) MakePublicKey() SM2 { 10 | this.publicKey = nil 11 | 12 | if this.privateKey == nil { 13 | err := errors.New("privateKey empty.") 14 | return this.AppendError(err) 15 | } 16 | 17 | this.publicKey = &this.privateKey.PublicKey 18 | 19 | return this 20 | } 21 | 22 | // 生成密钥 der 数据 23 | func (this SM2) MakeKeyDer() SM2 { 24 | var block *pem.Block 25 | if block, _ = pem.Decode(this.keyData); block == nil { 26 | err := errors.New("keyData error.") 27 | return this.AppendError(err) 28 | } 29 | 30 | this.keyData = block.Bytes 31 | 32 | return this 33 | } 34 | -------------------------------------------------------------------------------- /cryptobin/sm2/on.go: -------------------------------------------------------------------------------- 1 | package sm2 2 | 3 | type ( 4 | // 错误方法 5 | SM2ErrorFunc = func([]error) 6 | ) 7 | 8 | // 引出错误信息 9 | func (this SM2) OnError(fn SM2ErrorFunc) SM2 { 10 | fn(this.Errors) 11 | 12 | return this 13 | } 14 | 15 | -------------------------------------------------------------------------------- /cryptobin/sm2/to.go: -------------------------------------------------------------------------------- 1 | package sm2 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // 私钥/公钥 8 | func (this SM2) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // 私钥/公钥 13 | func (this SM2) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ========== 18 | 19 | // 输出字节 20 | func (this SM2) ToBytes() []byte { 21 | return this.parsedData 22 | } 23 | 24 | // 输出字符 25 | func (this SM2) ToString() string { 26 | return string(this.parsedData) 27 | } 28 | 29 | // 输出Base64 30 | func (this SM2) ToBase64String() string { 31 | return encoding.Base64Encode(this.parsedData) 32 | } 33 | 34 | // 输出Hex 35 | func (this SM2) ToHexString() string { 36 | return encoding.HexEncode(this.parsedData) 37 | } 38 | 39 | // ========== 40 | 41 | // 验证结果 42 | func (this SM2) ToVerify() bool { 43 | return this.verify 44 | } 45 | 46 | // 验证结果,返回 int 类型 47 | func (this SM2) ToVerifyInt() int { 48 | if this.verify { 49 | return 1 50 | } 51 | 52 | return 0 53 | } 54 | -------------------------------------------------------------------------------- /cryptobin/ssh/check.go: -------------------------------------------------------------------------------- 1 | package ssh 2 | 3 | import ( 4 | "crypto" 5 | "reflect" 6 | ) 7 | 8 | type keyEqual interface { 9 | Equal(x crypto.PublicKey) bool 10 | } 11 | 12 | // Check Key Pair 13 | func (this SSH) CheckKeyPair() bool { 14 | // get publicKey from privateKey 15 | pubKeyFromPriKey := this.MakePublicKey().publicKey 16 | 17 | if pubKeyFromPriKey == nil || this.publicKey == nil { 18 | return false 19 | } 20 | 21 | if pubkeyEqual, ok := pubKeyFromPriKey.(keyEqual); ok { 22 | if pubkeyEqual.Equal(this.publicKey) { 23 | return true 24 | } 25 | } 26 | 27 | if reflect.DeepEqual(pubKeyFromPriKey, this.publicKey) { 28 | return true 29 | } 30 | 31 | return false 32 | } 33 | -------------------------------------------------------------------------------- /cryptobin/ssh/error.go: -------------------------------------------------------------------------------- 1 | package ssh 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/errors" 5 | ) 6 | 7 | // Append one Error 8 | func (this SSH) AppendError(err ...error) SSH { 9 | this.Errors = append(this.Errors, err...) 10 | 11 | return this 12 | } 13 | 14 | // return Error 15 | func (this SSH) Error() error { 16 | return errors.Join(this.Errors...) 17 | } 18 | -------------------------------------------------------------------------------- /cryptobin/ssh/make.go: -------------------------------------------------------------------------------- 1 | package ssh 2 | 3 | import( 4 | "errors" 5 | "crypto/rsa" 6 | "crypto/dsa" 7 | "crypto/ecdsa" 8 | "crypto/ed25519" 9 | 10 | "github.com/deatil/go-cryptobin/gm/sm2" 11 | ) 12 | 13 | // Make PublicKey 14 | func (this SSH) MakePublicKey() SSH { 15 | this.publicKey = nil 16 | 17 | if this.privateKey == nil { 18 | err := errors.New("privateKey empty.") 19 | return this.AppendError(err) 20 | } 21 | 22 | switch prikey := this.privateKey.(type) { 23 | case *rsa.PrivateKey: 24 | this.publicKey = &prikey.PublicKey 25 | case *dsa.PrivateKey: 26 | this.publicKey = &prikey.PublicKey 27 | case *ecdsa.PrivateKey: 28 | this.publicKey = &prikey.PublicKey 29 | case ed25519.PrivateKey: 30 | this.publicKey = prikey.Public().(ed25519.PublicKey) 31 | case *sm2.PrivateKey: 32 | this.publicKey = &prikey.PublicKey 33 | } 34 | 35 | return this 36 | } 37 | -------------------------------------------------------------------------------- /cryptobin/ssh/on.go: -------------------------------------------------------------------------------- 1 | package ssh 2 | 3 | // On Error 4 | func (this SSH) OnError(fn func([]error)) SSH { 5 | fn(this.Errors) 6 | 7 | return this 8 | } 9 | 10 | -------------------------------------------------------------------------------- /cryptobin/ssh/to.go: -------------------------------------------------------------------------------- 1 | package ssh 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/encoding" 5 | ) 6 | 7 | // output Key Bytes 8 | func (this SSH) ToKeyBytes() []byte { 9 | return this.keyData 10 | } 11 | 12 | // output Key string 13 | func (this SSH) ToKeyString() string { 14 | return string(this.keyData) 15 | } 16 | 17 | // ========== 18 | 19 | // output Bytes 20 | func (this SSH) ToBytes() []byte { 21 | return this.parsedData 22 | } 23 | 24 | // output string 25 | func (this SSH) ToString() string { 26 | return string(this.parsedData) 27 | } 28 | 29 | // output Base64 30 | func (this SSH) ToBase64String() string { 31 | return encoding.Base64Encode(this.parsedData) 32 | } 33 | 34 | // output Hex 35 | func (this SSH) ToHexString() string { 36 | return encoding.HexEncode(this.parsedData) 37 | } 38 | 39 | // ========== 40 | 41 | // output verify 42 | func (this SSH) ToVerify() bool { 43 | return this.verify 44 | } 45 | 46 | // output verify int 47 | func (this SSH) ToVerifyInt() int { 48 | if this.verify { 49 | return 1 50 | } 51 | 52 | return 0 53 | } 54 | -------------------------------------------------------------------------------- /ecdh/key/curves.go: -------------------------------------------------------------------------------- 1 | package key 2 | 3 | import ( 4 | "encoding/asn1" 5 | 6 | "github.com/deatil/go-cryptobin/ecdh" 7 | ) 8 | 9 | type namedCurveInfo struct { 10 | namedCurve ecdh.Curve 11 | oid asn1.ObjectIdentifier 12 | } 13 | 14 | var namedCurves = make([]namedCurveInfo, 0) 15 | 16 | func AddNamedCurve(curve ecdh.Curve, oid asn1.ObjectIdentifier) { 17 | namedCurves = append(namedCurves, namedCurveInfo{ 18 | namedCurve: curve, 19 | oid: oid, 20 | }) 21 | } 22 | 23 | func NamedCurveFromOid(oid asn1.ObjectIdentifier) ecdh.Curve { 24 | for i := range namedCurves { 25 | cur := &namedCurves[i] 26 | if cur.oid.Equal(oid) { 27 | return cur.namedCurve 28 | } 29 | } 30 | 31 | return nil 32 | } 33 | 34 | func OidFromNamedCurve(curve ecdh.Curve) (asn1.ObjectIdentifier, bool) { 35 | for i := range namedCurves { 36 | cur := &namedCurves[i] 37 | if cur.namedCurve == curve { 38 | return cur.oid, true 39 | } 40 | } 41 | 42 | return asn1.ObjectIdentifier{}, false 43 | } 44 | -------------------------------------------------------------------------------- /ecdh/x25519.go: -------------------------------------------------------------------------------- 1 | package ecdh 2 | 3 | import ( 4 | "crypto/ecdh" 5 | ) 6 | 7 | func X25519() Curve { 8 | return defaultX25519 9 | } 10 | 11 | var defaultX25519 = NewNistCurve(ecdh.X25519()) 12 | -------------------------------------------------------------------------------- /elliptic/base_elliptic/elliptic.go: -------------------------------------------------------------------------------- 1 | // Package `base_elliptic` implements Elliptic curves over binary fields 2 | package base_elliptic 3 | 4 | // Create new elliptic curves over binary fields 5 | // warning: params dose not validated. 6 | // 7 | // `base_elliptic` uses `ellipse.CurveParams` for compatibility with `crypto.ellipse` package. 8 | // But do not use the functions of `ellipse.CurveParams`. It will be panic. 9 | func NewCurve(params *CurveParams) Curve { 10 | return &curve{ 11 | params: params, 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /elliptic/base_elliptic/test_test.go: -------------------------------------------------------------------------------- 1 | package base_elliptic 2 | 3 | import ( 4 | "math/big" 5 | ) 6 | 7 | var ( 8 | b233 = &curve{ 9 | params: &CurveParams{ 10 | Name: "B-233", 11 | BitSize: 233, 12 | F: F(233, 74, 0), 13 | A: HI("0x000000000000000000000000000000000000000000000000000000000001"), 14 | B: HI("0x0066647ede6c332c7f8c0923bb58213b333b20e9ce4281fe115f7d8f90ad"), 15 | Gx: HI("0x00fac9dfcbac8313bb2139f1bb755fef65bc391f8b36f8f8eb7371fd558b"), 16 | Gy: HI("0x01006a08a41903350678e58528bebf8a0beff867a7ca36716f7e01f81052"), 17 | N: HI("0x1000000000000000000000000000013e974e72f8a6922031d2603cfe0d7"), 18 | H: 0x2, 19 | }, 20 | } 21 | ) 22 | 23 | type internalTestcase struct { 24 | x1, y1 *big.Int 25 | x2, y2 *big.Int 26 | x, y *big.Int 27 | } 28 | -------------------------------------------------------------------------------- /elliptic/bign/bign.go: -------------------------------------------------------------------------------- 1 | package bign 2 | 3 | import ( 4 | "sync" 5 | "encoding/asn1" 6 | "crypto/elliptic" 7 | ) 8 | 9 | var ( 10 | OIDNamedCurveP256v1 = asn1.ObjectIdentifier{1, 2, 112, 0, 2, 0, 34, 101, 45, 3, 1} 11 | OIDNamedCurveP384v1 = asn1.ObjectIdentifier{1, 2, 112, 0, 2, 0, 34, 101, 45, 3, 2} 12 | OIDNamedCurveP512v1 = asn1.ObjectIdentifier{1, 2, 112, 0, 2, 0, 34, 101, 45, 3, 3} 13 | ) 14 | 15 | var initonce sync.Once 16 | 17 | func P256v1() elliptic.Curve { 18 | initonce.Do(initAll) 19 | return p256v1 20 | } 21 | 22 | func P384v1() elliptic.Curve { 23 | initonce.Do(initAll) 24 | return p384v1 25 | } 26 | 27 | func P512v1() elliptic.Curve { 28 | initonce.Do(initAll) 29 | return p512v1 30 | } 31 | -------------------------------------------------------------------------------- /elliptic/bign/curve/fiat/Dockerfile: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | FROM coqorg/coq:8.20.0 6 | 7 | RUN git clone https://github.com/mit-plv/fiat-crypto && cd fiat-crypto && \ 8 | git submodule update --init --recursive 9 | RUN cd fiat-crypto && eval $(opam env) && make standalone-ocaml SKIP_BEDROCK2=1 10 | 11 | ENV PATH /home/coq/fiat-crypto/src/ExtractionOCaml:$PATH 12 | -------------------------------------------------------------------------------- /elliptic/curve256k1/table.go: -------------------------------------------------------------------------------- 1 | package curve256k1 2 | 3 | import ( 4 | "strconv" 5 | "crypto/subtle" 6 | ) 7 | 8 | type lookupTable struct { 9 | points [15]PointJacobian 10 | } 11 | 12 | func (v *lookupTable) Init(p *PointJacobian) { 13 | points := &v.points 14 | points[0].Set(p) 15 | for i := 1; i < 15; i += 2 { 16 | points[i].Double(&points[i/2]) 17 | points[i+1].Add(&points[i], p) 18 | } 19 | } 20 | 21 | // x must be in [0, 15]. 22 | func (v *lookupTable) SelectInto(dest *PointJacobian, x uint8) { 23 | if x >= 16 { 24 | panic("curve256k1: out-of-bounds: " + strconv.Itoa(int(x))) 25 | } 26 | dest.Zero() 27 | for i := uint8(1); i < 16; i++ { 28 | cond := subtle.ConstantTimeByteEq(x, i) 29 | dest.Select(&v.points[i-1], dest, cond) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /elliptic/koblitz/koblitz.go: -------------------------------------------------------------------------------- 1 | package koblitz 2 | 3 | import ( 4 | "encoding/asn1" 5 | "crypto/elliptic" 6 | 7 | "github.com/deatil/go-cryptobin/elliptic/bitcurves" 8 | ) 9 | 10 | // Support for Koblitz elliptic curves 11 | // http://www.secg.org/SEC2-Ver-1.0.pdf 12 | 13 | var ( 14 | OIDNamedCurveSecp160k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 9} 15 | OIDNamedCurveSecp192k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 31} 16 | OIDNamedCurveSecp224k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 32} 17 | OIDNamedCurveSecp256k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 10} 18 | ) 19 | 20 | func P160k1() elliptic.Curve { 21 | return bitcurves.S160() 22 | } 23 | 24 | func P192k1() elliptic.Curve { 25 | return bitcurves.S192() 26 | } 27 | 28 | func P224k1() elliptic.Curve { 29 | return bitcurves.S224() 30 | } 31 | 32 | func P256k1() elliptic.Curve { 33 | return bitcurves.S256() 34 | } 35 | -------------------------------------------------------------------------------- /elliptic/koblitz/koblitz_test.go: -------------------------------------------------------------------------------- 1 | package koblitz 2 | 3 | import ( 4 | "testing" 5 | "crypto/rand" 6 | "crypto/ecdsa" 7 | "crypto/elliptic" 8 | ) 9 | 10 | func testCurve(t *testing.T, curve elliptic.Curve) { 11 | priv, err := ecdsa.GenerateKey(curve, rand.Reader) 12 | if err != nil { 13 | t.Fatal(err) 14 | } 15 | 16 | msg := []byte("test") 17 | r, s, err := ecdsa.Sign(rand.Reader, priv, msg) 18 | if err != nil { 19 | t.Fatal(err) 20 | } 21 | 22 | if !ecdsa.Verify(&priv.PublicKey, msg, r, s) { 23 | t.Fatal("signature didn't verify.") 24 | } 25 | } 26 | 27 | func TestP160k1(t *testing.T) { 28 | testCurve(t, P160k1()) 29 | } 30 | 31 | func TestP192k1(t *testing.T) { 32 | testCurve(t, P192k1()) 33 | } 34 | 35 | func TestP224k1(t *testing.T) { 36 | testCurve(t, P224k1()) 37 | } 38 | 39 | func TestP256k1(t *testing.T) { 40 | testCurve(t, P256k1()) 41 | } 42 | -------------------------------------------------------------------------------- /elliptic/tom/tom.go: -------------------------------------------------------------------------------- 1 | package tom 2 | 3 | import ( 4 | "sync" 5 | "encoding/asn1" 6 | "crypto/elliptic" 7 | ) 8 | 9 | var ( 10 | OIDTom = asn1.ObjectIdentifier{1, 2, 999, 1, 1, 1} 11 | 12 | OIDNamedCurveTom256 = asn1.ObjectIdentifier{1, 2, 999, 1, 1, 1, 1} 13 | OIDNamedCurveTom384 = asn1.ObjectIdentifier{1, 2, 999, 1, 1, 1, 2} 14 | OIDNamedCurveTom521 = asn1.ObjectIdentifier{1, 2, 999, 1, 1, 1, 3} 15 | ) 16 | 17 | // sync.Once variable to ensure initialization occurs only once 18 | var once sync.Once 19 | 20 | func P256() elliptic.Curve { 21 | once.Do(initAll) 22 | return p256 23 | } 24 | 25 | func P384() elliptic.Curve { 26 | once.Do(initAll) 27 | return p384 28 | } 29 | 30 | func P521() elliptic.Curve { 31 | once.Do(initAll) 32 | return p521 33 | } 34 | -------------------------------------------------------------------------------- /elliptic/tom/tom_test.go: -------------------------------------------------------------------------------- 1 | package tom 2 | 3 | import ( 4 | "testing" 5 | "crypto/rand" 6 | "crypto/ecdsa" 7 | "crypto/elliptic" 8 | ) 9 | 10 | func testCurve(t *testing.T, curve elliptic.Curve) { 11 | priv, err := ecdsa.GenerateKey(curve, rand.Reader) 12 | if err != nil { 13 | t.Fatal(err) 14 | } 15 | 16 | msg := []byte("test") 17 | r, s, err := ecdsa.Sign(rand.Reader, priv, msg) 18 | if err != nil { 19 | t.Fatal(err) 20 | } 21 | 22 | if !ecdsa.Verify(&priv.PublicKey, msg, r, s) { 23 | t.Fatal("signature didn't verify.") 24 | } 25 | } 26 | 27 | type testData struct { 28 | name string 29 | curve elliptic.Curve 30 | } 31 | 32 | func Test_Curve(t *testing.T) { 33 | tests := []testData{ 34 | {"P256", P256()}, 35 | {"P384", P384()}, 36 | // {"P521", P521()}, 37 | } 38 | 39 | for _, c := range tests { 40 | t.Run(c.name, func(t *testing.T) { 41 | testCurve(t, c.curve) 42 | }) 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /elliptic/weierstrass/weierstrass.go: -------------------------------------------------------------------------------- 1 | package weierstrass 2 | 3 | import ( 4 | "encoding/asn1" 5 | "crypto/elliptic" 6 | ) 7 | 8 | // Support for short Weierstrass elliptic curves 9 | // http://www.secg.org/SEC2-Ver-1.0.pdf 10 | 11 | var ( 12 | OIDNamedCurveSecp160k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 9} 13 | OIDNamedCurveSecp160r1 = asn1.ObjectIdentifier{1, 3, 132, 0, 8} 14 | OIDNamedCurveSecp160r2 = asn1.ObjectIdentifier{1, 3, 132, 0, 30} 15 | OIDNamedCurveSecp192k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 31} 16 | OIDNamedCurveSecp192r1 = asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 1} 17 | OIDNamedCurveSecp224k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 32} 18 | OIDNamedCurveSecp256k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 10} 19 | ) 20 | 21 | func P160r1() elliptic.Curve { 22 | once.Do(initAll) 23 | return p160r1 24 | } 25 | 26 | func P160r2() elliptic.Curve { 27 | once.Do(initAll) 28 | return p160r2 29 | } 30 | 31 | func P192r1() elliptic.Curve { 32 | once.Do(initAll) 33 | return p192r1 34 | } 35 | -------------------------------------------------------------------------------- /elliptic/weierstrass/weierstrass_test.go: -------------------------------------------------------------------------------- 1 | package weierstrass 2 | 3 | import ( 4 | "testing" 5 | "crypto/rand" 6 | "crypto/ecdsa" 7 | "crypto/elliptic" 8 | ) 9 | 10 | func testCurve(t *testing.T, curve elliptic.Curve) { 11 | priv, err := ecdsa.GenerateKey(curve, rand.Reader) 12 | if err != nil { 13 | t.Fatal(err) 14 | } 15 | 16 | msg := []byte("test") 17 | r, s, err := ecdsa.Sign(rand.Reader, priv, msg) 18 | if err != nil { 19 | t.Fatal(err) 20 | } 21 | 22 | if !ecdsa.Verify(&priv.PublicKey, msg, r, s) { 23 | t.Fatal("signature didn't verify.") 24 | } 25 | } 26 | 27 | func TestP160r1(t *testing.T) { 28 | testCurve(t, P160r1()) 29 | } 30 | 31 | func TestP160r2(t *testing.T) { 32 | testCurve(t, P160r2()) 33 | } 34 | 35 | func TestP192r1(t *testing.T) { 36 | testCurve(t, P192r1()) 37 | } 38 | 39 | -------------------------------------------------------------------------------- /gm/sm2/compress.go: -------------------------------------------------------------------------------- 1 | package sm2 2 | 3 | import ( 4 | "errors" 5 | 6 | "github.com/deatil/go-cryptobin/gm/sm2/sm2curve" 7 | ) 8 | 9 | // 解缩公钥 10 | // Decompress PublicKey data 11 | func Decompress(data []byte) (*PublicKey, error) { 12 | c := P256() 13 | 14 | x, y := sm2curve.UnmarshalCompressed(c, data) 15 | if x == nil || y == nil { 16 | return nil, errors.New("go-cryptobin/sm2: compress publicKey is incorrect.") 17 | } 18 | 19 | pub := &PublicKey{ 20 | Curve: c, 21 | X: x, 22 | Y: y, 23 | } 24 | 25 | return pub, nil 26 | } 27 | 28 | // 压缩公钥 29 | // Compress PublicKey struct 30 | func Compress(pub *PublicKey) []byte { 31 | return sm2curve.MarshalCompressed(pub.Curve, pub.X, pub.Y) 32 | } 33 | -------------------------------------------------------------------------------- /gm/sm2/sm2curve/field/field_test.go: -------------------------------------------------------------------------------- 1 | package field_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/deatil/go-cryptobin/gm/sm2/sm2curve/field" 7 | ) 8 | 9 | func BenchmarkMul(b *testing.B) { 10 | v := new(field.Element).One() 11 | b.ReportAllocs() 12 | b.ResetTimer() 13 | for i := 0; i < b.N; i++ { 14 | v.Mul(v, v) 15 | } 16 | } 17 | 18 | func BenchmarkSquare(b *testing.B) { 19 | v := new(field.Element).One() 20 | b.ReportAllocs() 21 | b.ResetTimer() 22 | for i := 0; i < b.N; i++ { 23 | v.Square(v) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /gm/sm2/sm2curve/p256.go: -------------------------------------------------------------------------------- 1 | package sm2curve 2 | 3 | import ( 4 | "sync" 5 | "crypto/elliptic" 6 | ) 7 | 8 | var initonce sync.Once 9 | 10 | var p256 = &sm2Curve{ 11 | newPoint: NewPoint, 12 | } 13 | 14 | func initP256() { 15 | p256.params = &elliptic.CurveParams{ 16 | Name: "sm2p256v1", 17 | BitSize: 256, 18 | P: bigFromHex("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF"), 19 | N: bigFromHex("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123"), 20 | B: bigFromHex("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93"), 21 | Gx: bigFromHex("32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7"), 22 | Gy: bigFromHex("BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0"), 23 | } 24 | } 25 | 26 | func P256() elliptic.Curve { 27 | initonce.Do(initP256) 28 | return p256 29 | } 30 | -------------------------------------------------------------------------------- /gm/sm2/sm2curve/sm2ec_asm_table.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/gm/sm2/sm2curve/sm2ec_asm_table.bin -------------------------------------------------------------------------------- /gm/sm2/utils.go: -------------------------------------------------------------------------------- 1 | package sm2 2 | 3 | import ( 4 | "math/big" 5 | "crypto/subtle" 6 | "crypto/elliptic" 7 | "encoding/binary" 8 | ) 9 | 10 | func intToBytes(x int) []byte { 11 | var buf = make([]byte, 4) 12 | binary.BigEndian.PutUint32(buf, uint32(x)) 13 | 14 | return buf 15 | } 16 | 17 | func bigIntToBytes(curve elliptic.Curve, value *big.Int) []byte { 18 | byteLen := (curve.Params().BitSize + 7) / 8 19 | 20 | buf := make([]byte, byteLen) 21 | value.FillBytes(buf) 22 | 23 | return buf 24 | } 25 | 26 | func bytesToBigInt(value []byte) *big.Int { 27 | return new(big.Int).SetBytes(value) 28 | } 29 | 30 | // bigIntEqual reports whether a and b are equal leaking only their bit length 31 | // through timing side-channels. 32 | func bigIntEqual(a, b *big.Int) bool { 33 | return subtle.ConstantTimeCompare(a.Bytes(), b.Bytes()) == 1 34 | } 35 | -------------------------------------------------------------------------------- /gm/sm9/sm9curve/gfp_decl.go: -------------------------------------------------------------------------------- 1 | //go:build (amd64 && !purego) || (arm64 && !purego) 2 | 3 | package sm9curve 4 | 5 | // This file contains forward declarations for the architecture-specific 6 | // assembly implementations of these functions, provided that they exist. 7 | 8 | import ( 9 | "golang.org/x/sys/cpu" 10 | ) 11 | 12 | var hasBMI2 = cpu.X86.HasBMI2 13 | 14 | //go:noescape 15 | func gfpNeg(c, a *gfP) 16 | 17 | //go:noescape 18 | func gfpAdd(c, a, b *gfP) 19 | 20 | //go:noescape 21 | func gfpSub(c, a, b *gfP) 22 | 23 | //go:noescape 24 | func gfpMul(c, a, b *gfP) 25 | -------------------------------------------------------------------------------- /gm/zuc/eea_test.go: -------------------------------------------------------------------------------- 1 | package zuc 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func Test_NewZuc(t *testing.T) { 8 | key := []byte("test-passg5tyhu8") 9 | iv := []byte("test-p66sg5tyhu8") 10 | 11 | in := []byte("test-passg5tyhu8test-passg5tyhu8test-passg5tyhu8test-passg5tyhu8test-passg5tyhu8test-passg5tyhu8test-passg5tyhu8test-passg5tyhu8test-passg5tyhu8test-passg5tyhu8test-passg5tyhu8test-passg5tyhu8") 12 | 13 | s := NewZucEncrypt(key, iv) 14 | 15 | out := make([]byte, len(in)) 16 | s.Write(in, out) 17 | other := s.Sum(nil) 18 | 19 | copy(out[len(out)-len(other):], other) 20 | 21 | if len(out) == 0 { 22 | t.Error("NewZucEncrypt make error") 23 | } 24 | 25 | // =========== 26 | 27 | s2 := NewZucDecrypt(key, iv) 28 | 29 | out2 := make([]byte, len(out)) 30 | s2.Write(out, out2) 31 | other2 := s2.Sum(nil) 32 | 33 | copy(out2[len(out2)-len(other2):], other2) 34 | 35 | if len(out2) == 0 { 36 | t.Error("NewZucDecrypt make error") 37 | } 38 | 39 | if string(out2) != string(in) { 40 | t.Error("Zuc Decrypt error") 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /gm/zuc/zuc256_test.go: -------------------------------------------------------------------------------- 1 | package zuc 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func Test_Zuc256(t *testing.T) { 8 | key := []byte("test-passg5tyhu8test-passg5tyhu8") 9 | iv := []byte("test-passg5tyhu8test-pa") 10 | 11 | in := []byte("test-passg5tyhu8test-passg5tyhu8test-passg5tyhu8test-passg5tyhu8") 12 | 13 | s := NewZuc256State(key, iv) 14 | 15 | out := make([]byte, len(in)) 16 | s.Encrypt(out, in) 17 | 18 | if len(out) == 0 { 19 | t.Error("Zuc make error") 20 | } 21 | 22 | // =========== 23 | 24 | s2 := NewZuc256State(key, iv) 25 | 26 | out2 := make([]byte, len(in)) 27 | s2.Encrypt(out2, out) 28 | 29 | if len(out) == 0 { 30 | t.Error("Zuc make 2 error") 31 | } 32 | 33 | if string(out2) != string(in) { 34 | t.Error("Zuc Decrypt error") 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/deatil/go-cryptobin 2 | 3 | go 1.20 4 | 5 | require ( 6 | golang.org/x/crypto v0.31.0 7 | golang.org/x/text v0.21.0 8 | ) 9 | 10 | require golang.org/x/sys v0.28.0 11 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= 2 | golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= 3 | golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= 4 | golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 5 | golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q= 6 | golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= 7 | golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= 8 | -------------------------------------------------------------------------------- /hash/bash/sbox.go: -------------------------------------------------------------------------------- 1 | package bash 2 | 3 | var bash_rc = [BASH_ROUNDS]uint64{ 4 | 0x3bf5080ac8ba94b1, 5 | 0xc1d1659c1bbd92f6, 6 | 0x60e8b2ce0ddec97b, 7 | 0xec5fb8fe790fbc13, 8 | 0xaa043de6436706a7, 9 | 0x8929ff6a5e535bfd, 10 | 0x98bf1e2c50c97550, 11 | 0x4c5f8f162864baa8, 12 | 0x262fc78b14325d54, 13 | 0x1317e3c58a192eaa, 14 | 0x098bf1e2c50c9755, 15 | 0xd8ee19681d669304, 16 | 0x6c770cb40eb34982, 17 | 0x363b865a0759a4c1, 18 | 0xc73622b47c4c0ace, 19 | 0x639b115a3e260567, 20 | 0xede6693460f3da1d, 21 | 0xaad8d5034f9935a0, 22 | 0x556c6a81a7cc9ad0, 23 | 0x2ab63540d3e64d68, 24 | 0x155b1aa069f326b4, 25 | 0x0aad8d5034f9935a, 26 | 0x0556c6a81a7cc9ad, 27 | 0xde8082cd72debc78, 28 | } 29 | 30 | var bash_rot = [BASH_ROT_ROUNDS][BASH_ROT_IDX]byte{ 31 | { 8, 53, 14, 1 }, 32 | { 56, 51, 34, 7 }, 33 | { 8, 37, 46, 49 }, 34 | { 56, 3, 2, 23 }, 35 | { 8, 21, 14, 33 }, 36 | { 56, 19, 34, 39 }, 37 | { 8, 5, 46, 17 }, 38 | { 56, 35, 2, 55 }, 39 | } 40 | -------------------------------------------------------------------------------- /hash/belt/belt.go: -------------------------------------------------------------------------------- 1 | package belt 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | /* 8 | * This is an implementation of the BELT-HASH hash function as 9 | * defined int STB 34.101.31. 10 | */ 11 | 12 | // New returns a new hash.Hash computing the BELT checksum 13 | func New() hash.Hash { 14 | return newDigest() 15 | } 16 | 17 | // Sum returns the BELT checksum of the data. 18 | func Sum(data []byte) (sum [Size]byte) { 19 | h := New() 20 | h.Write(data) 21 | hashed := h.Sum(nil) 22 | 23 | copy(sum[:], hashed[:Size]) 24 | return 25 | } 26 | -------------------------------------------------------------------------------- /hash/cmac/utils.go: -------------------------------------------------------------------------------- 1 | package cmac 2 | 3 | import ( 4 | "crypto/subtle" 5 | ) 6 | 7 | func shift(dst, src []byte) int { 8 | var b, bit byte 9 | for i := len(src) - 1; i >= 0; i-- { // a range would be nice 10 | bit = src[i] >> 7 11 | dst[i] = src[i]<<1 | b 12 | b = bit 13 | } 14 | 15 | return int(b) 16 | } 17 | 18 | // XOR the contents of b into a in-place 19 | func xor(a, b []byte) { 20 | subtle.XORBytes(a, a, b) 21 | } 22 | -------------------------------------------------------------------------------- /hash/cmac/verify.go: -------------------------------------------------------------------------------- 1 | package cmac 2 | 3 | import ( 4 | "crypto/cipher" 5 | "crypto/subtle" 6 | ) 7 | 8 | // Sum computes the CMAC checksum with the given tagsize of msg using the cipher.Block. 9 | func Sum(msg []byte, c cipher.Block, tagsize int) ([]byte, error) { 10 | h, err := NewWithTagSize(c, tagsize) 11 | if err != nil { 12 | return nil, err 13 | } 14 | 15 | h.Write(msg) 16 | return h.Sum(nil), nil 17 | } 18 | 19 | // Verify computes the CMAC checksum with the given tagsize of msg and compares 20 | // it with the given mac. This functions returns true if and only if the given mac 21 | // is equal to the computed one. 22 | func Verify(mac, msg []byte, c cipher.Block, tagsize int) bool { 23 | sum, err := Sum(msg, c, tagsize) 24 | if err != nil { 25 | return false 26 | } 27 | 28 | return subtle.ConstantTimeCompare(mac, sum) == 1 29 | } 30 | -------------------------------------------------------------------------------- /hash/gost/gost34112012256/hash.go: -------------------------------------------------------------------------------- 1 | package gost34112012256 2 | 3 | import ( 4 | "hash" 5 | 6 | "github.com/deatil/go-cryptobin/hash/gost/gost34112012" 7 | ) 8 | 9 | // GOST R 34.11-2012 256-bit hash function. 10 | // RFC 6986. Big-endian hash output. 11 | 12 | const ( 13 | Size = 32 14 | BlockSize = gost34112012.BlockSize 15 | ) 16 | 17 | func New() hash.Hash { 18 | return gost34112012.New(32) 19 | } 20 | -------------------------------------------------------------------------------- /hash/gost/gost34112012256/kdf.go: -------------------------------------------------------------------------------- 1 | package gost34112012256 2 | 3 | import ( 4 | "hash" 5 | "crypto/hmac" 6 | ) 7 | 8 | func KDF(h func() hash.Hash, key, label, seed []byte) (r []byte) { 9 | fn := hmac.New(h, key) 10 | 11 | if _, err := fn.Write([]byte{0x01}); err != nil { 12 | panic(err) 13 | } 14 | if _, err := fn.Write(label); err != nil { 15 | panic(err) 16 | } 17 | 18 | if _, err := fn.Write([]byte{0x00}); err != nil { 19 | panic(err) 20 | } 21 | if _, err := fn.Write(seed); err != nil { 22 | panic(err) 23 | } 24 | 25 | if _, err := fn.Write([]byte{0x01}); err != nil { 26 | panic(err) 27 | } 28 | if _, err := fn.Write([]byte{0x00}); err != nil { 29 | panic(err) 30 | } 31 | 32 | r = fn.Sum(nil) 33 | 34 | return r 35 | } 36 | -------------------------------------------------------------------------------- /hash/gost/gost34112012512/hash.go: -------------------------------------------------------------------------------- 1 | package gost34112012512 2 | 3 | import ( 4 | "hash" 5 | 6 | "github.com/deatil/go-cryptobin/hash/gost/gost34112012" 7 | ) 8 | 9 | // GOST R 34.11-2012 512-bit hash function. 10 | // RFC 6986. Big-endian hash output. 11 | 12 | const ( 13 | Size = 64 14 | BlockSize = gost34112012.BlockSize 15 | ) 16 | 17 | func New() hash.Hash { 18 | return gost34112012.New(64) 19 | } 20 | -------------------------------------------------------------------------------- /hash/has160/has160.go: -------------------------------------------------------------------------------- 1 | package has160 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New returns a new hash.Hash computing the HAS-160 checksum. 8 | func New() hash.Hash { 9 | return newDigest() 10 | } 11 | 12 | // Sum returns the HAS-160 checksum of the data. 13 | func Sum(data []byte) (sum [Size]byte) { 14 | var h digest 15 | h.Reset() 16 | h.Write(data) 17 | 18 | hash := h.Sum(nil) 19 | copy(sum[:], hash) 20 | return 21 | } 22 | -------------------------------------------------------------------------------- /hash/has160/sbox.go: -------------------------------------------------------------------------------- 1 | package has160 2 | 3 | var ( 4 | initH = [...]uint32{ 5 | 0x67452301, 6 | 0xefcdab89, 7 | 0x98badcfe, 8 | 0x10325476, 9 | 0xc3d2e1f0, 10 | } 11 | 12 | s1 = [...]int{5, 11, 7, 15, 6, 13, 8, 14, 7, 12, 9, 11, 8, 15, 6, 12, 9, 14, 5, 13} 13 | 14 | x1 = [...]int{18, 0, 1, 2, 3, 19, 4, 5, 6, 7, 16, 8, 9, 10, 11, 17, 12, 13, 14, 15} 15 | x2 = [...]int{18, 3, 6, 9, 12, 19, 15, 2, 5, 8, 16, 11, 14, 1, 4, 17, 7, 10, 13, 0} 16 | x3 = [...]int{18, 12, 5, 14, 7, 19, 0, 9, 2, 11, 16, 4, 13, 6, 15, 17, 8, 1, 10, 3} 17 | x4 = [...]int{18, 7, 2, 13, 8, 19, 3, 14, 9, 4, 16, 15, 10, 5, 0, 17, 11, 6, 1, 12} 18 | ) 19 | -------------------------------------------------------------------------------- /hash/md2/md2.go: -------------------------------------------------------------------------------- 1 | package md2 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New returns a new hash.Hash computing the MD2 checksum. 8 | func New() hash.Hash { 9 | return newDigest() 10 | } 11 | 12 | // Sum returns checksum of the data. 13 | func Sum(data []byte) (sum [Size]byte) { 14 | var h digest 15 | h.Reset() 16 | h.Write(data) 17 | 18 | hash := h.Sum(nil) 19 | copy(sum[:], hash) 20 | return 21 | } 22 | -------------------------------------------------------------------------------- /hash/pmac/utils.go: -------------------------------------------------------------------------------- 1 | package pmac 2 | 3 | import ( 4 | "crypto/subtle" 5 | ) 6 | 7 | // XOR the contents of b into a in-place 8 | func xor(a, b []byte) { 9 | subtle.XORBytes(a, a, b) 10 | } 11 | -------------------------------------------------------------------------------- /hash/pmac/verify.go: -------------------------------------------------------------------------------- 1 | package pmac 2 | 3 | import ( 4 | "crypto/cipher" 5 | "crypto/subtle" 6 | ) 7 | 8 | // Sum computes the PMAC checksum with the given tagsize of msg using the cipher.Block. 9 | func Sum(msg []byte, c cipher.Block, tagsize int) ([]byte, error) { 10 | h, err := NewWithTagSize(c, tagsize) 11 | if err != nil { 12 | return nil, err 13 | } 14 | 15 | h.Write(msg) 16 | return h.Sum(nil), nil 17 | } 18 | 19 | // Verify computes the PMAC checksum with the given tagsize of msg and compares 20 | // it with the given mac. This functions returns true if and only if the given mac 21 | // is equal to the computed one. 22 | func Verify(mac, msg []byte, c cipher.Block, tagsize int) bool { 23 | sum, err := Sum(msg, c, tagsize) 24 | if err != nil { 25 | return false 26 | } 27 | 28 | return subtle.ConstantTimeCompare(mac, sum) == 1 29 | } 30 | -------------------------------------------------------------------------------- /hash/rabin/poly_test.go: -------------------------------------------------------------------------------- 1 | package rabin 2 | 3 | import "testing" 4 | 5 | func TestPolyMod(t *testing.T) { 6 | var p polyGF2 7 | for _, test := range [][3]uint64{ 8 | {0x3e75, 0x3e75, 0x0}, // a(x) mod a(x) = 0 9 | {0x3e75 << 1, 0x3e75, 0x0}, // a(x)*x mod a(x) = 0 10 | {0x3e74, 0x3e75, 0x1}, // a(x) + 1 mod a(x) = 1 11 | {0x7337, 0xe39b, 0x7337}, // degree(a) < degree(b) 12 | // Random polynomials, checked with Wolfram Alpha. 13 | {0x3e75, 0x201b, 0x1e6e}, 14 | {0xd10b, 0x35f7, 0x6d7}, 15 | {0xe5a2, 0x8c83, 0x6921}, 16 | {0x9a4a, 0xa8c7, 0x328d}, 17 | } { 18 | a, b := newPolyGF2(test[0]), newPolyGF2(test[1]) 19 | p.Mod(a, b) 20 | if p.coeff.Uint64() != test[2] { 21 | t.Errorf("%s mod %s = %s (%#x), want %s", a, b, &p, p.coeff.Uint64(), newPolyGF2(test[2])) 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /hash/sm3/block_amd64.go: -------------------------------------------------------------------------------- 1 | //go:build !purego 2 | 3 | package sm3 4 | 5 | import "golang.org/x/sys/cpu" 6 | 7 | // 汇编部分来自于 github.com/emmansun/gmsm/sm3 8 | 9 | var useAVX2 = cpu.X86.HasAVX2 && cpu.X86.HasBMI2 10 | var useAVX = cpu.X86.HasAVX 11 | var useSSSE3 = cpu.X86.HasSSSE3 12 | 13 | //go:noescape 14 | func blockAMD64(dig *digest, p []byte) 15 | 16 | //go:noescape 17 | func blockSIMD(dig *digest, p []byte) 18 | 19 | //go:noescape 20 | func blockAVX2(dig *digest, p []byte) 21 | 22 | func block(dig *digest, p []byte) { 23 | if useAVX2 { 24 | blockAVX2(dig, p) 25 | } else if useSSSE3 || useAVX { 26 | blockSIMD(dig, p) 27 | } else { 28 | blockAMD64(dig, p) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /hash/sm3/block_arm64.go: -------------------------------------------------------------------------------- 1 | //go:build !purego 2 | 3 | package sm3 4 | 5 | import ( 6 | "os" 7 | "golang.org/x/sys/cpu" 8 | ) 9 | 10 | // 汇编部分来自于 github.com/emmansun/gmsm/sm3 11 | 12 | var useSM3NI = cpu.ARM64.HasSM3 && os.Getenv("DISABLE_SM3NI") != "1" 13 | 14 | var t = []uint32{ 15 | 0x79cc4519, 16 | 0x9d8a7a87, 17 | } 18 | 19 | //go:noescape 20 | func blockARM64(dig *digest, p []byte) 21 | 22 | //go:noescape 23 | func blockSM3NI(h []uint32, p []byte, t []uint32) 24 | 25 | func block(dig *digest, p []byte) { 26 | if !useSM3NI { 27 | blockARM64(dig, p) 28 | } else { 29 | h := dig.s[:] 30 | blockSM3NI(h, p, t) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /hash/sm3/block_generic.go: -------------------------------------------------------------------------------- 1 | //go:build purego || !(amd64 || arm64) 2 | 3 | package sm3 4 | 5 | func block(dig *digest, p []byte) { 6 | blockGeneric(dig, p) 7 | } 8 | -------------------------------------------------------------------------------- /hash/sm3/rand_test.go: -------------------------------------------------------------------------------- 1 | package sm3 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func Test_Rand(t *testing.T) { 8 | nonce := []byte("nonce-asdfg") 9 | label := []byte("label-asdfg") 10 | addin := []byte("addin-ftgyj") 11 | 12 | out := make([]byte, 16) 13 | 14 | r := NewRand(nonce, label) 15 | r.Generate(out, addin) 16 | 17 | if len(out) == 0 { 18 | t.Error("Rand make error") 19 | } 20 | 21 | if len(out) != 16 { 22 | t.Error("Rand make length error") 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /hash/sm3/sbox.go: -------------------------------------------------------------------------------- 1 | package sm3 2 | 3 | var iv = [8]uint32{ 4 | 0x7380166F, 0x4914B2B9, 0x172442D7, 0xDA8A0600, 5 | 0xA96F30BC, 0x163138AA, 0xE38DEE4D, 0xB0FB0E4E, 6 | } 7 | 8 | var tSbox = [64]uint32{ 9 | 0x79cc4519, 0xf3988a32, 0xe7311465, 0xce6228cb, 10 | 0x9cc45197, 0x3988a32f, 0x7311465e, 0xe6228cbc, 11 | 0xcc451979, 0x988a32f3, 0x311465e7, 0x6228cbce, 12 | 0xc451979c, 0x88a32f39, 0x11465e73, 0x228cbce6, 13 | 0x9d8a7a87, 0x3b14f50f, 0x7629ea1e, 0xec53d43c, 14 | 0xd8a7a879, 0xb14f50f3, 0x629ea1e7, 0xc53d43ce, 15 | 0x8a7a879d, 0x14f50f3b, 0x29ea1e76, 0x53d43cec, 16 | 0xa7a879d8, 0x4f50f3b1, 0x9ea1e762, 0x3d43cec5, 17 | 0x7a879d8a, 0xf50f3b14, 0xea1e7629, 0xd43cec53, 18 | 0xa879d8a7, 0x50f3b14f, 0xa1e7629e, 0x43cec53d, 19 | 0x879d8a7a, 0x0f3b14f5, 0x1e7629ea, 0x3cec53d4, 20 | 0x79d8a7a8, 0xf3b14f50, 0xe7629ea1, 0xcec53d43, 21 | 0x9d8a7a87, 0x3b14f50f, 0x7629ea1e, 0xec53d43c, 22 | 0xd8a7a879, 0xb14f50f3, 0x629ea1e7, 0xc53d43ce, 23 | 0x8a7a879d, 0x14f50f3b, 0x29ea1e76, 0x53d43cec, 24 | 0xa7a879d8, 0x4f50f3b1, 0x9ea1e762, 0x3d43cec5, 25 | } 26 | -------------------------------------------------------------------------------- /hash/sm3/sm3.go: -------------------------------------------------------------------------------- 1 | package sm3 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New returns a new hash.Hash computing the SM3 checksum. 8 | func New() hash.Hash { 9 | return newDigest() 10 | } 11 | 12 | // Sum returns the SM3 checksum of the data. 13 | func Sum(data []byte) (sum [Size]byte) { 14 | h := New() 15 | h.Write(data) 16 | hash := h.Sum(nil) 17 | 18 | copy(sum[:], hash) 19 | return 20 | } 21 | -------------------------------------------------------------------------------- /jceks/bks_utils.go: -------------------------------------------------------------------------------- 1 | package jceks 2 | 3 | import ( 4 | "crypto/x509" 5 | ) 6 | 7 | // parseCertChain 8 | func parseCertChain(certChain [][]byte) (certs []*x509.Certificate, err error) { 9 | for _, cert := range certChain { 10 | var parsedCert *x509.Certificate 11 | parsedCert, err = x509.ParseCertificate(cert) 12 | if err != nil { 13 | return 14 | } 15 | 16 | certs = append(certs, parsedCert) 17 | } 18 | 19 | return 20 | } 21 | 22 | -------------------------------------------------------------------------------- /jceks/cipher.go: -------------------------------------------------------------------------------- 1 | package jceks 2 | 3 | import( 4 | "github.com/deatil/go-cryptobin/padding" 5 | "github.com/deatil/go-cryptobin/tool/utils" 6 | ) 7 | 8 | var newPadding = padding.NewPKCS7() 9 | 10 | // 明文补码算法 11 | func pkcs7Padding(text []byte, blockSize int) []byte { 12 | return newPadding.Padding(text, blockSize) 13 | } 14 | 15 | // 明文减码算法 16 | func pkcs7UnPadding(src []byte) ([]byte, error) { 17 | return newPadding.UnPadding(src) 18 | } 19 | 20 | // 随机生成字符 21 | func genRandom(num int) ([]byte, error) { 22 | return utils.GenRandom(num) 23 | } 24 | -------------------------------------------------------------------------------- /jceks/consts.go: -------------------------------------------------------------------------------- 1 | package jceks 2 | 3 | import( 4 | "encoding/asn1" 5 | ) 6 | 7 | const ( 8 | certType = "X.509" 9 | ) 10 | 11 | const ( 12 | jceksMagic = 0xcececece 13 | jceksVersion = 0x02 14 | 15 | jceksPrivateKeyId = 1 16 | jceksTrustedCertId = 2 17 | jceksSecretKeyId = 3 18 | ) 19 | 20 | const ( 21 | jksMagic = 0xFEEDFEED 22 | jksVersion = 0x02 23 | 24 | jksPrivateKeyId = 1 25 | jksTrustedCertId = 2 26 | ) 27 | 28 | var ( 29 | // JavaSoft proprietary key-protection algorithm (used to protect 30 | // private keys in the keystore implementation that comes with JDK 31 | // 1.2). 32 | oidKeyProtector = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 42, 2, 17, 1, 1} 33 | ) 34 | -------------------------------------------------------------------------------- /jceks/jceks.go: -------------------------------------------------------------------------------- 1 | package jceks 2 | 3 | import ( 4 | "io" 5 | "bytes" 6 | ) 7 | 8 | /** 9 | * JCEKS 10 | * 11 | * @create 2022-9-19 12 | * @author deatil 13 | */ 14 | type JCEKS struct { 15 | // 解析后数据 16 | entries map[string]any 17 | } 18 | 19 | // 构造函数 20 | func NewJCEKS() *JCEKS { 21 | return &JCEKS{ 22 | entries: make(map[string]any), 23 | } 24 | } 25 | 26 | // LoadJceksFromReader loads the key store from the specified file. 27 | func LoadJceksFromReader(reader io.Reader, password string) (*JCEKS, error) { 28 | ks := &JCEKS{ 29 | entries: make(map[string]any), 30 | } 31 | 32 | err := ks.Parse(reader, password) 33 | if err != nil { 34 | return nil, err 35 | } 36 | 37 | return ks, err 38 | } 39 | 40 | // LoadJceksFromBytes loads the key store from the bytes data. 41 | func LoadJceksFromBytes(data []byte, password string) (*JCEKS, error) { 42 | buf := bytes.NewReader(data) 43 | 44 | return LoadJceksFromReader(buf, password) 45 | } 46 | 47 | // 别名 48 | var LoadFromReader = LoadJceksFromReader 49 | var LoadFromBytes = LoadJceksFromBytes 50 | var NewJceksEncode = NewJCEKS 51 | -------------------------------------------------------------------------------- /jceks/testdata/bks/christmas.bksv1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/bks/christmas.bksv1 -------------------------------------------------------------------------------- /jceks/testdata/bks/christmas.bksv2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/bks/christmas.bksv2 -------------------------------------------------------------------------------- /jceks/testdata/bks/custom_entry_passwords.bksv1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/bks/custom_entry_passwords.bksv1 -------------------------------------------------------------------------------- /jceks/testdata/bks/custom_entry_passwords.bksv2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/bks/custom_entry_passwords.bksv2 -------------------------------------------------------------------------------- /jceks/testdata/bks/empty.bksv1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/bks/empty.bksv1 -------------------------------------------------------------------------------- /jceks/testdata/bks/empty.bksv2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/bks/empty.bksv2 -------------------------------------------------------------------------------- /jceks/testdata/jceks/example-custom-oid.jceks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/jceks/example-custom-oid.jceks -------------------------------------------------------------------------------- /jceks/testdata/jceks/example-elliptic-sha1.jceks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/jceks/example-elliptic-sha1.jceks -------------------------------------------------------------------------------- /jceks/testdata/jceks/example-root.jceks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/jceks/example-root.jceks -------------------------------------------------------------------------------- /jceks/testdata/jceks/example-sha1.jceks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/jceks/example-sha1.jceks -------------------------------------------------------------------------------- /jceks/testdata/jceks/example-small-key.jceks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/jceks/example-small-key.jceks -------------------------------------------------------------------------------- /jceks/testdata/jks/DSA_1024_keystore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/jks/DSA_1024_keystore.jks -------------------------------------------------------------------------------- /jceks/testdata/jks/EC_256_keystore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/jks/EC_256_keystore.jks -------------------------------------------------------------------------------- /jceks/testdata/jks/RSA_2048_MD5withRSA_keystore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/jks/RSA_2048_MD5withRSA_keystore.jks -------------------------------------------------------------------------------- /jceks/testdata/jks/RSA_2048_SHA1withRSA_keystore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/jks/RSA_2048_SHA1withRSA_keystore.jks -------------------------------------------------------------------------------- /jceks/testdata/jks/RSA_2048_SHA256withRSA_keystore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/jks/RSA_2048_SHA256withRSA_keystore.jks -------------------------------------------------------------------------------- /jceks/testdata/jks/RSA_2048_keystore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/jks/RSA_2048_keystore.jks -------------------------------------------------------------------------------- /jceks/testdata/jks/RSA_2048_truststore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/jks/RSA_2048_truststore.jks -------------------------------------------------------------------------------- /jceks/testdata/jks/androidstudio_default_123456_myalias.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/jks/androidstudio_default_123456_myalias.jks -------------------------------------------------------------------------------- /jceks/testdata/jks/diff_pass.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/jks/diff_pass.jks -------------------------------------------------------------------------------- /jceks/testdata/jks/windows_defaults_123456_mykey.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/jks/windows_defaults_123456_mykey.jks -------------------------------------------------------------------------------- /jceks/testdata/uber/christmas.uber: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/uber/christmas.uber -------------------------------------------------------------------------------- /jceks/testdata/uber/custom_entry_passwords.uber: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/uber/custom_entry_passwords.uber -------------------------------------------------------------------------------- /jceks/testdata/uber/empty.uber: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/jceks/testdata/uber/empty.uber -------------------------------------------------------------------------------- /kdf/argon2/blamka_ref.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | //go:build !amd64 || purego || !gc 6 | 7 | package argon2 8 | 9 | func processBlock(out, in1, in2 *block) { 10 | processBlockGeneric(out, in1, in2, false) 11 | } 12 | 13 | func processBlockXOR(out, in1, in2 *block) { 14 | processBlockGeneric(out, in1, in2, true) 15 | } 16 | -------------------------------------------------------------------------------- /kdf/pbkdf2/prf.go: -------------------------------------------------------------------------------- 1 | package pbkdf2 2 | 3 | import ( 4 | "hash" 5 | "crypto/hmac" 6 | "crypto/cipher" 7 | 8 | "github.com/deatil/go-cryptobin/hash/cmac" 9 | ) 10 | 11 | type PRF interface { 12 | NewHash(key []byte) hash.Hash 13 | } 14 | 15 | type HmacPRF struct { 16 | Hash func() hash.Hash 17 | } 18 | 19 | func NewHmacPRF(h func() hash.Hash) HmacPRF { 20 | return HmacPRF{ 21 | Hash: h, 22 | } 23 | } 24 | 25 | func (prf HmacPRF) NewHash(key []byte) hash.Hash { 26 | return hmac.New(prf.Hash, key) 27 | } 28 | 29 | type CmacPRF struct { 30 | Cipher func(key []byte) (cipher.Block, error) 31 | } 32 | 33 | func NewCmacPRF(cip func(key []byte) (cipher.Block, error)) CmacPRF { 34 | return CmacPRF{ 35 | Cipher: cip, 36 | } 37 | } 38 | 39 | func (prf CmacPRF) NewHash(key []byte) hash.Hash { 40 | cip, err := prf.Cipher(key) 41 | if err != nil { 42 | panic(err.Error()) 43 | } 44 | 45 | hasher, err := cmac.New(cip) 46 | if err != nil { 47 | panic(err.Error()) 48 | } 49 | 50 | return hasher 51 | } 52 | 53 | -------------------------------------------------------------------------------- /kdf/smkdf/smkdf.go: -------------------------------------------------------------------------------- 1 | package smkdf 2 | 3 | import ( 4 | "hash" 5 | "encoding/binary" 6 | ) 7 | 8 | // Kdf key derivation function, compliance with GB/T 32918.4-2016 5.4.3. 9 | // ANSI-X9.63-KDF 10 | func Key(h func() hash.Hash, z []byte, size int) []byte { 11 | md := h() 12 | mdSize := md.Size() 13 | 14 | limit := (uint64(size) + uint64(mdSize) - 1) / uint64(mdSize) 15 | if limit >= uint64(1 << 32) - 1 { 16 | panic("go-cryptobin/smkdf: key length too long") 17 | } 18 | 19 | var countBytes [4]byte 20 | var count uint32 = 1 21 | var k []byte 22 | 23 | for i := uint64(0); i < limit; i++ { 24 | binary.BigEndian.PutUint32(countBytes[:], count) 25 | 26 | md.Reset() 27 | md.Write(z) 28 | md.Write(countBytes[:]) 29 | k = md.Sum(k) 30 | 31 | count++ 32 | } 33 | 34 | return k[:size] 35 | } 36 | -------------------------------------------------------------------------------- /kdf/smkdf/smkdf_64bit_test.go: -------------------------------------------------------------------------------- 1 | //go:build amd64 || amd64p32 || arm64 || loong64 || mips64le || ppc64le || riscv64 2 | // +build amd64 amd64p32 arm64 loong64 mips64le ppc64le riscv64 3 | 4 | package smkdf 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/deatil/go-cryptobin/hash/sm3" 10 | ) 11 | 12 | func Test_64bit_Fail(t *testing.T) { 13 | defer func() { 14 | if err := recover(); err == nil { 15 | t.Error("should throw panic") 16 | } 17 | }() 18 | 19 | _ = Key(sm3.New, []byte("cryptobin-test"), 1 << 38) 20 | } 21 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-cryptobin/cc78e7946ca4099b97cff1a2595532b7ab94b0a2/logo.png -------------------------------------------------------------------------------- /mac/mac.go: -------------------------------------------------------------------------------- 1 | package mac 2 | 3 | import ( 4 | "crypto/cipher" 5 | ) 6 | 7 | // Reference: GB/T 15821.1-2020 Security techniques 8 | // Message authentication codes - Part 1: Mechanisms using block ciphers 9 | 10 | // BlockCipherMAC is the interface that wraps the basic MAC method. 11 | type BlockCipherMAC interface { 12 | // Size returns the MAC value's number of bytes. 13 | Size() int 14 | 15 | // MAC calculates the MAC of the given data. 16 | // The MAC value's number of bytes is returned by Size. 17 | // Intercept message authentication code as needed. 18 | MAC(src []byte) []byte 19 | } 20 | 21 | // BlockCipherFunc is creator func type 22 | type BlockCipherFunc = func(key []byte) (cipher.Block, error) 23 | -------------------------------------------------------------------------------- /mode/bc_test.go: -------------------------------------------------------------------------------- 1 | package mode 2 | 3 | import ( 4 | "testing" 5 | "crypto/aes" 6 | 7 | cryptobin_test "github.com/deatil/go-cryptobin/tool/test" 8 | ) 9 | 10 | func Test_BC(t *testing.T) { 11 | assertEqual := cryptobin_test.AssertEqualT(t) 12 | assertNoError := cryptobin_test.AssertNoErrorT(t) 13 | assertNotEmpty := cryptobin_test.AssertNotEmptyT(t) 14 | 15 | key := []byte("kkinjkijeel22plo") 16 | iv := []byte("11injkijkol22plo") 17 | plaintext := []byte("kjinjkijkolkdplo") 18 | 19 | c, err := aes.NewCipher(key) 20 | assertNoError(err, "Test_BC") 21 | 22 | mode := NewBCEncrypter(c, iv) 23 | ciphertext := make([]byte, len(plaintext)) 24 | mode.CryptBlocks(ciphertext, plaintext) 25 | 26 | mode2 := NewBCDecrypter(c, iv) 27 | plaintext2 := make([]byte, len(ciphertext)) 28 | mode2.CryptBlocks(plaintext2, ciphertext) 29 | 30 | assertNotEmpty(plaintext2, "Test_BC") 31 | 32 | assertEqual(plaintext2, plaintext, "Test_BC-Equal") 33 | } 34 | -------------------------------------------------------------------------------- /mode/hctr/hctr_test.go: -------------------------------------------------------------------------------- 1 | package hctr 2 | 3 | import ( 4 | "testing" 5 | "crypto/aes" 6 | 7 | "github.com/deatil/go-cryptobin/tool/test" 8 | ) 9 | 10 | func Test_HCTR(t *testing.T) { 11 | assertEqual := test.AssertEqualT(t) 12 | assertNoError := test.AssertNoErrorT(t) 13 | assertNotEmpty := test.AssertNotEmptyT(t) 14 | 15 | key := []byte("kkinjkijeel22plo") 16 | plaintext := []byte("kjinjkijkolkdplo") 17 | 18 | tweak := []byte("kkinjkijeel2pass") 19 | hkey := []byte("11injkijkol22plo") 20 | 21 | c, err := aes.NewCipher(key) 22 | assertNoError(err, "NewHCTR") 23 | 24 | mode := NewHCTR(c, tweak, hkey) 25 | ciphertext := make([]byte, len(plaintext)) 26 | mode.Encrypt(ciphertext, plaintext) 27 | 28 | mode2 := NewHCTR(c, tweak, hkey) 29 | plaintext2 := make([]byte, len(ciphertext)) 30 | mode2.Decrypt(plaintext2, ciphertext) 31 | 32 | assertNotEmpty(plaintext2, "NewHCTR") 33 | 34 | assertEqual(plaintext2, plaintext, "NewHCTR-Equal") 35 | } 36 | -------------------------------------------------------------------------------- /mode/ige_test.go: -------------------------------------------------------------------------------- 1 | package mode 2 | 3 | import ( 4 | "testing" 5 | "crypto/aes" 6 | 7 | cryptobin_test "github.com/deatil/go-cryptobin/tool/test" 8 | ) 9 | 10 | func Test_IGE(t *testing.T) { 11 | assertEqual := cryptobin_test.AssertEqualT(t) 12 | assertNoError := cryptobin_test.AssertNoErrorT(t) 13 | assertNotEmpty := cryptobin_test.AssertNotEmptyT(t) 14 | 15 | key := []byte("kkinjkijeel22plo") 16 | iv := []byte("11injkijkol22plo11injkijkol22plo") 17 | plaintext := []byte("kjinjkijkolkdplo") 18 | 19 | c, err := aes.NewCipher(key) 20 | assertNoError(err, "Test_IGE") 21 | 22 | mode := NewIGEEncrypter(c, iv) 23 | ciphertext := make([]byte, len(plaintext)) 24 | mode.CryptBlocks(ciphertext, plaintext) 25 | 26 | mode2 := NewIGEDecrypter(c, iv) 27 | plaintext2 := make([]byte, len(ciphertext)) 28 | mode2.CryptBlocks(plaintext2, ciphertext) 29 | 30 | assertNotEmpty(plaintext2, "Test_IGE") 31 | 32 | assertEqual(plaintext2, plaintext, "Test_IGE-Equal") 33 | } 34 | -------------------------------------------------------------------------------- /mode/ocb/rfc7253_test_vectors_suite_b.go: -------------------------------------------------------------------------------- 1 | package ocb 2 | 3 | // Second set of test vectors from https://tools.ietf.org/html/rfc7253 4 | var rfc7253TestVectorTaglen96 = struct { 5 | key, nonce, header, plaintext, ciphertext string 6 | }{"0F0E0D0C0B0A09080706050403020100", 7 | "BBAA9988776655443322110D", 8 | "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627", 9 | "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627", 10 | "1792A4E31E0755FB03E31B22116E6C2DDF9EFD6E33D536F1A0124B0A55BAE884ED93481529C76B6AD0C515F4D1CDD4FDAC4F02AA"} 11 | 12 | var rfc7253AlgorithmTest = []struct { 13 | KEYLEN, TAGLEN int 14 | OUTPUT string 15 | }{ 16 | {128, 128, "67E944D23256C5E0B6C61FA22FDF1EA2"}, 17 | {192, 128, "F673F2C3E7174AAE7BAE986CA9F29E17"}, 18 | {256, 128, "D90EB8E9C977C88B79DD793D7FFA161C"}, 19 | {128, 96, "77A3D8E73589158D25D01209"}, 20 | {192, 96, "05D56EAD2752C86BE6932C5E"}, 21 | {256, 96, "5458359AC23B0CBA9E6330DD"}, 22 | {128, 64, "192C9B7BD90BA06A"}, 23 | {192, 64, "0066BC6E0EF34E24"}, 24 | {256, 64, "7D4EA5D445501CBE"}, 25 | } 26 | -------------------------------------------------------------------------------- /mode/ofb8_test.go: -------------------------------------------------------------------------------- 1 | package mode 2 | 3 | import ( 4 | "testing" 5 | "crypto/aes" 6 | 7 | cryptobin_test "github.com/deatil/go-cryptobin/tool/test" 8 | ) 9 | 10 | func Test_OFB8(t *testing.T) { 11 | assertEqual := cryptobin_test.AssertEqualT(t) 12 | assertNoError := cryptobin_test.AssertNoErrorT(t) 13 | assertNotEmpty := cryptobin_test.AssertNotEmptyT(t) 14 | 15 | key := []byte("kkinjkijeel22plo") 16 | iv := []byte("11injkijkol22plo") 17 | plaintext := []byte("kjinjkijkolkdplo") 18 | 19 | c, err := aes.NewCipher(key) 20 | assertNoError(err, "Test_OFB8") 21 | 22 | mode := NewOFB8(c, iv) 23 | ciphertext := make([]byte, len(plaintext)) 24 | mode.XORKeyStream(ciphertext, plaintext) 25 | 26 | mode2 := NewOFB8(c, iv) 27 | plaintext2 := make([]byte, len(ciphertext)) 28 | mode2.XORKeyStream(plaintext2, ciphertext) 29 | 30 | assertNotEmpty(plaintext2, "Test_OFB8") 31 | 32 | assertEqual(plaintext2, plaintext, "Test_OFB8-Equal") 33 | } 34 | -------------------------------------------------------------------------------- /mode/ofbnlf_test.go: -------------------------------------------------------------------------------- 1 | package mode 2 | 3 | import ( 4 | "testing" 5 | "crypto/aes" 6 | 7 | cryptobin_test "github.com/deatil/go-cryptobin/tool/test" 8 | ) 9 | 10 | func Test_OFBNLF(t *testing.T) { 11 | assertEqual := cryptobin_test.AssertEqualT(t) 12 | assertNotEmpty := cryptobin_test.AssertNotEmptyT(t) 13 | 14 | key := []byte("kkinjkijeel22plo") 15 | iv := []byte("11injkijkol22plo") 16 | plaintext := []byte("kjinjkijkolkdplo") 17 | 18 | mode := NewOFBNLFEncrypter(aes.NewCipher, key, iv) 19 | ciphertext := make([]byte, len(plaintext)) 20 | mode.CryptBlocks(ciphertext, plaintext) 21 | 22 | mode2 := NewOFBNLFDecrypter(aes.NewCipher, key, iv) 23 | plaintext2 := make([]byte, len(ciphertext)) 24 | mode2.CryptBlocks(plaintext2, ciphertext) 25 | 26 | assertNotEmpty(plaintext2, "Test_OFBNLF") 27 | 28 | assertEqual(plaintext2, plaintext, "Test_OFBNLF-Equal") 29 | } 30 | -------------------------------------------------------------------------------- /padding/iso10126.go: -------------------------------------------------------------------------------- 1 | package padding 2 | 3 | import ( 4 | "errors" 5 | ) 6 | 7 | /** 8 | * ISO10126 补码 9 | * 10 | * @create 2024-12-5 11 | * @author deatil 12 | */ 13 | type ISO10126 struct {} 14 | 15 | // 构造函数 16 | func NewISO10126() ISO10126 { 17 | return ISO10126{} 18 | } 19 | 20 | // ISO10126Padding 21 | // 填充至符合块大小的整数倍,填充值最后一个字节为填充的数量数,其他字节填充随机字节。 22 | func (this ISO10126) Padding(text []byte, blockSize int) []byte { 23 | n := len(text) 24 | if blockSize < 1 { 25 | return text 26 | } 27 | 28 | // 补位 blockSize 值 29 | paddingSize := blockSize - n%blockSize 30 | paddingText := randomBytes(uint(paddingSize - 1)) 31 | 32 | text = append(text, paddingText...) 33 | text = append(text, byte(paddingSize)) 34 | 35 | return text 36 | } 37 | 38 | func (this ISO10126) UnPadding(src []byte) ([]byte, error) { 39 | n := len(src) 40 | if n == 0 { 41 | return nil, errors.New("invalid data length") 42 | } 43 | 44 | num := n - int(src[n-1]) 45 | if num < 0 { 46 | return nil, errors.New("invalid padding") 47 | } 48 | 49 | return src[:num], nil 50 | } 51 | -------------------------------------------------------------------------------- /padding/padding.go: -------------------------------------------------------------------------------- 1 | package padding 2 | 3 | import ( 4 | "math/rand" 5 | ) 6 | 7 | // padding interface struct 8 | type Padding interface { 9 | // Padding func 10 | Padding(text []byte, blockSize int) []byte 11 | 12 | // UnPadding func 13 | UnPadding(src []byte) ([]byte, error) 14 | } 15 | 16 | // 随机字节 17 | func randomBytes(length uint) []byte { 18 | charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" 19 | 20 | b := make([]byte, length) 21 | for i := range b { 22 | b[i] = charset[rand.Int63()%int64(len(charset))] 23 | } 24 | 25 | return b 26 | } 27 | -------------------------------------------------------------------------------- /padding/zero.go: -------------------------------------------------------------------------------- 1 | package padding 2 | 3 | import ( 4 | "bytes" 5 | ) 6 | 7 | /** 8 | * Zero 补码 9 | * 10 | * @create 2024-12-5 11 | * @author deatil 12 | */ 13 | type Zero struct {} 14 | 15 | // 构造函数 16 | func NewZero() Zero { 17 | return Zero{} 18 | } 19 | 20 | // 数据长度不对齐时使用0填充,否则不填充 21 | func (this Zero) Padding(text []byte, blockSize int) []byte { 22 | n := len(text) 23 | if blockSize < 1 { 24 | return text 25 | } 26 | 27 | // 补位 blockSize 值 28 | paddingSize := blockSize - n%blockSize 29 | paddingText := bytes.Repeat([]byte{byte(0)}, paddingSize) 30 | 31 | return append(text, paddingText...) 32 | } 33 | 34 | func (this Zero) UnPadding(src []byte) ([]byte, error) { 35 | return bytes.TrimRight(src, string([]byte{0})), nil 36 | } 37 | -------------------------------------------------------------------------------- /pkcs/pbes1/cipher_derived_key.go: -------------------------------------------------------------------------------- 1 | package pbes1 2 | 3 | import ( 4 | "hash" 5 | 6 | "github.com/deatil/go-cryptobin/kdf/pbkdf" 7 | ) 8 | 9 | // 生成密钥 10 | func DerivedKeyPkcs12(password string, salt string, iter int, keyLen int, ivLen int, h func() hash.Hash) ([]byte, []byte) { 11 | key := pbkdf.Key(h, 20, 64, []byte(salt), []byte(password), iter, 1, keyLen) 12 | iv := pbkdf.Key(h, 20, 64, []byte(salt), []byte(password), iter, 2, ivLen) 13 | 14 | return key, iv 15 | } 16 | 17 | // 生成密钥 18 | func DerivedKeyPbkdf1(password string, salt string, iter int, keyLen int, ivLen int, h func() hash.Hash) ([]byte, []byte) { 19 | key := hashKey(h, []byte(password + salt)) 20 | 21 | for i := 0; i < iter - 1; i++ { 22 | key = hashKey(h, key) 23 | } 24 | 25 | return key[:keyLen], key[keyLen:keyLen+ivLen] 26 | } 27 | 28 | // 单个加密 29 | func hashKey(h func() hash.Hash, key []byte) []byte { 30 | fn := h() 31 | fn.Write(key) 32 | 33 | return fn.Sum(nil) 34 | } 35 | -------------------------------------------------------------------------------- /pkcs12/bmp_string.go: -------------------------------------------------------------------------------- 1 | package pkcs12 2 | 3 | import ( 4 | "github.com/deatil/go-cryptobin/tool/bmp_string" 5 | ) 6 | 7 | var ( 8 | // BmpStringZeroTerminated returns s encoded in UCS-2 with a zero terminator. 9 | bmpStringZeroTerminated = bmp_string.BmpStringZeroTerminated 10 | 11 | // BmpString returns s encoded in UCS-2 12 | bmpString = bmp_string.BmpString 13 | 14 | // DecodeBMPString return utf-8 string 15 | decodeBMPString = bmp_string.DecodeBMPString 16 | ) 17 | -------------------------------------------------------------------------------- /pkcs12/cipher.go: -------------------------------------------------------------------------------- 1 | package pkcs12 2 | 3 | import( 4 | "github.com/deatil/go-cryptobin/pkcs/pbes1" 5 | ) 6 | 7 | // Add Cipher and Get Cipher 8 | var ( 9 | AddCipher = pbes1.AddCipher 10 | GetCipher = pbes1.GetCipher 11 | ) 12 | 13 | // Cipher list 14 | var ( 15 | CipherSHA1And3DES = pbes1.SHA1And3DES 16 | CipherSHA1And2DES = pbes1.SHA1And2DES 17 | CipherSHA1AndRC2_128 = pbes1.SHA1AndRC2_128 18 | CipherSHA1AndRC2_40 = pbes1.SHA1AndRC2_40 19 | CipherSHA1AndRC4_128 = pbes1.SHA1AndRC4_128 20 | CipherSHA1AndRC4_40 = pbes1.SHA1AndRC4_40 21 | 22 | CipherMD5AndCAST5 = pbes1.MD5AndCAST5 23 | CipherSHAAndTwofish = pbes1.SHAAndTwofish 24 | ) 25 | -------------------------------------------------------------------------------- /pkcs12/enveloped/interfaces.go: -------------------------------------------------------------------------------- 1 | package enveloped 2 | 3 | import ( 4 | "crypto" 5 | "encoding/asn1" 6 | ) 7 | 8 | // 非对称加密 9 | type KeyEncrypt interface { 10 | // oid 11 | OID() asn1.ObjectIdentifier 12 | 13 | // 加密, 返回: [加密后数据, error] 14 | Encrypt(plaintext []byte, pkey crypto.PublicKey) ([]byte, error) 15 | 16 | // 解密 17 | Decrypt(ciphertext []byte, pkey crypto.PrivateKey) ([]byte, error) 18 | } 19 | 20 | var keyens = make(map[string]func() KeyEncrypt) 21 | 22 | // 添加 key 加密方式 23 | func AddkeyEncrypt(oid asn1.ObjectIdentifier, fn func() KeyEncrypt) { 24 | keyens[oid.String()] = fn 25 | } 26 | -------------------------------------------------------------------------------- /pkcs12/errors.go: -------------------------------------------------------------------------------- 1 | package pkcs12 2 | 3 | import "errors" 4 | 5 | var ( 6 | // ErrDecryption represents a failure to decrypt the input. 7 | ErrDecryption = errors.New("go-cryptobin/pkcs12: decryption error, incorrect padding") 8 | 9 | // ErrIncorrectPassword is returned when an incorrect password is detected. 10 | // Usually, P12/PFX data is signed to be able to verify the password. 11 | ErrIncorrectPassword = errors.New("go-cryptobin/pkcs12: decryption password incorrect") 12 | ) 13 | 14 | // NotImplementedError indicates that the input is not currently supported. 15 | type NotImplementedError string 16 | 17 | func (e NotImplementedError) Error() string { 18 | return "go-cryptobin/pkcs12: " + string(e) 19 | } 20 | -------------------------------------------------------------------------------- /pkcs12/key_setting.go: -------------------------------------------------------------------------------- 1 | package pkcs12 2 | 3 | import ( 4 | "crypto/dsa" 5 | "crypto/rsa" 6 | "crypto/ecdsa" 7 | "crypto/ed25519" 8 | 9 | "github.com/deatil/go-cryptobin/gm/sm2" 10 | "github.com/deatil/go-cryptobin/pubkey/gost" 11 | ) 12 | 13 | func init() { 14 | AddKey(GetStructName(&dsa.PrivateKey{}), func() Key { 15 | return new(KeyDSA) 16 | }) 17 | AddKey(GetStructName(&rsa.PrivateKey{}), func() Key { 18 | return new(KeyRsa) 19 | }) 20 | AddKey(GetStructName(&ecdsa.PrivateKey{}), func() Key { 21 | return new(KeyEcdsa) 22 | }) 23 | AddKey(GetStructName(ed25519.PrivateKey{}), func() Key { 24 | return new(KeyEdDSA) 25 | }) 26 | AddKey(GetStructName(&sm2.PrivateKey{}), func() Key { 27 | return new(KeySM2) 28 | }) 29 | AddKey(GetStructName(&gost.PrivateKey{}), func() Key { 30 | return new(KeyGost) 31 | }) 32 | } 33 | -------------------------------------------------------------------------------- /pkcs12/mac_test.go: -------------------------------------------------------------------------------- 1 | package pkcs12 2 | 3 | import ( 4 | "testing" 5 | "encoding/asn1" 6 | 7 | cryptobin_test "github.com/deatil/go-cryptobin/tool/test" 8 | ) 9 | 10 | func Test_hashByOID_fail(t *testing.T) { 11 | assertEqual := cryptobin_test.AssertEqualT(t) 12 | 13 | oidFail := asn1.ObjectIdentifier{1, 222, 643, 777, 12, 13, 5, 1} 14 | 15 | _, _, err := hashByOID(oidFail) 16 | if err == nil { 17 | t.Error("should throw panic") 18 | } 19 | 20 | check := "go-cryptobin/pkcs12: unsupported hash (OID: 1.222.643.777.12.13.5.1)" 21 | assertEqual(err.Error(), check, "Test_hashByOID_fail") 22 | } 23 | 24 | func Test_prfByOIDPBMAC1_fail(t *testing.T) { 25 | assertEqual := cryptobin_test.AssertEqualT(t) 26 | 27 | oidFail := asn1.ObjectIdentifier{1, 222, 643, 777, 12, 13, 5, 1} 28 | 29 | _, err := pbmac1PRFByOID(oidFail) 30 | if err == nil { 31 | t.Error("should throw panic") 32 | } 33 | 34 | check := "go-cryptobin/pkcs12: unsupported hash (OID: 1.222.643.777.12.13.5.1)" 35 | assertEqual(err.Error(), check, "Test_hashByOID_fail") 36 | } 37 | 38 | -------------------------------------------------------------------------------- /pkcs7/hasher.go: -------------------------------------------------------------------------------- 1 | package pkcs7 2 | 3 | import ( 4 | "hash" 5 | "encoding/asn1" 6 | ) 7 | 8 | // hash 9 | type SignHashWithFunc struct { 10 | hashFunc func() hash.Hash 11 | identifier asn1.ObjectIdentifier 12 | } 13 | 14 | // oid 15 | func (this SignHashWithFunc) OID() asn1.ObjectIdentifier { 16 | return this.identifier 17 | } 18 | 19 | // hash checksum 20 | func (this SignHashWithFunc) Sum(data []byte) []byte { 21 | h := this.hashFunc() 22 | h.Write(data) 23 | newData := h.Sum(nil) 24 | 25 | return newData 26 | } 27 | -------------------------------------------------------------------------------- /pkcs7/signer.go: -------------------------------------------------------------------------------- 1 | package pkcs7 2 | 3 | import ( 4 | "hash" 5 | "crypto" 6 | ) 7 | 8 | // 通用加密 9 | func hashSignData(hashType crypto.Hash, data []byte) []byte { 10 | h := hashType.New() 11 | h.Write(data) 12 | hash := h.Sum(nil) 13 | 14 | return hash 15 | } 16 | 17 | // 通用加密 18 | func hashFuncSignData(hashType func() hash.Hash, data []byte) []byte { 19 | h := hashType() 20 | h.Write(data) 21 | hash := h.Sum(nil) 22 | 23 | return hash 24 | } 25 | 26 | -------------------------------------------------------------------------------- /pkcs7/utils.go: -------------------------------------------------------------------------------- 1 | package pkcs7 2 | 3 | import ( 4 | "errors" 5 | "encoding/pem" 6 | ) 7 | 8 | // 编码到 pem 9 | // pemType = [PKCS7 | ENCRYPTED PKCS7] 10 | func EncodePkcs7ToPem(data []byte, pemType string) []byte { 11 | if pemType == "" { 12 | pemType = "PKCS7" 13 | } 14 | 15 | keyBlock := &pem.Block{ 16 | Type: pemType, 17 | Bytes: data, 18 | } 19 | 20 | return pem.EncodeToMemory(keyBlock) 21 | } 22 | 23 | // 解析 pkcs7 pem 数据 24 | func ParsePkcs7Pem(data []byte) ([]byte, error) { 25 | var block *pem.Block 26 | if block, _ = pem.Decode(data); block == nil { 27 | return nil, errors.New("go-cryptobin/pkcs7: data is not pem") 28 | } 29 | 30 | return block.Bytes, nil 31 | } 32 | -------------------------------------------------------------------------------- /pkcs8/pbes2/kdf.go: -------------------------------------------------------------------------------- 1 | package pbes2 2 | 3 | import ( 4 | "encoding/asn1" 5 | ) 6 | 7 | // KDF options interface 8 | type KDFOpts interface { 9 | // Salt Size 10 | GetSaltSize() int 11 | 12 | // oid 13 | OID() asn1.ObjectIdentifier 14 | 15 | // PBES oid 16 | PBESOID() asn1.ObjectIdentifier 17 | 18 | // with HasKeyLength option 19 | WithHasKeyLength(hasKeyLength bool) KDFOpts 20 | 21 | // DeriveKey 22 | DeriveKey(password, salt []byte, size int) (key []byte, params KDFParameters, err error) 23 | } 24 | 25 | // KDFParameters 26 | type KDFParameters interface { 27 | // PBES oid 28 | PBESOID() asn1.ObjectIdentifier 29 | 30 | // DeriveKey 31 | DeriveKey(password []byte, size int) (key []byte, err error) 32 | } 33 | 34 | var kdfs = make(map[string]func() KDFParameters) 35 | 36 | // add kdf type 37 | func AddKDF(oid asn1.ObjectIdentifier, params func() KDFParameters) { 38 | kdfs[oid.String()] = params 39 | } 40 | -------------------------------------------------------------------------------- /pubkey/bign/curves.go: -------------------------------------------------------------------------------- 1 | package bign 2 | 3 | import ( 4 | "encoding/asn1" 5 | "crypto/elliptic" 6 | ) 7 | 8 | type namedCurveInfo struct { 9 | namedCurve elliptic.Curve 10 | oid asn1.ObjectIdentifier 11 | } 12 | 13 | var namedCurves = make([]namedCurveInfo, 0) 14 | 15 | func AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) { 16 | namedCurves = append(namedCurves, namedCurveInfo{ 17 | namedCurve: curve, 18 | oid: oid, 19 | }) 20 | } 21 | 22 | func NamedCurveFromOid(oid asn1.ObjectIdentifier) elliptic.Curve { 23 | for i := range namedCurves { 24 | cur := &namedCurves[i] 25 | if cur.oid.Equal(oid) { 26 | return cur.namedCurve 27 | } 28 | } 29 | 30 | return nil 31 | } 32 | 33 | func OidFromNamedCurve(curve elliptic.Curve) (asn1.ObjectIdentifier, bool) { 34 | for i := range namedCurves { 35 | cur := &namedCurves[i] 36 | if cur.namedCurve == curve { 37 | return cur.oid, true 38 | } 39 | } 40 | 41 | return asn1.ObjectIdentifier{}, false 42 | } 43 | -------------------------------------------------------------------------------- /pubkey/bign/utils_test.go: -------------------------------------------------------------------------------- 1 | package bign 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | ) 7 | 8 | func Test_MakeAdata(t *testing.T) { 9 | oid := []byte("123") 10 | typ := []byte("33") 11 | 12 | adata := MakeAdata(oid, typ) 13 | 14 | oid2, err := GetOidFromAdata(adata) 15 | if err != nil { 16 | t.Fatal(err) 17 | } 18 | 19 | typ2, err := GetTFromAdata(adata) 20 | if err != nil { 21 | t.Fatal(err) 22 | } 23 | 24 | if !bytes.Equal(oid2, oid) { 25 | t.Errorf("get oid fail, got: %x, want: %x", oid2, oid) 26 | } 27 | if !bytes.Equal(typ2, typ) { 28 | t.Errorf("get type fail, got: %x, want: %x", typ2, typ) 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /pubkey/bip0340/batch.go: -------------------------------------------------------------------------------- 1 | package bip0340 2 | 3 | /* 4 | * BIP0340 batch verification functions. 5 | */ 6 | func BatchVerify(pub []*PublicKey, m, sig [][]byte, hashFunc Hasher) bool { 7 | u := len(pub) 8 | 9 | if len(m) == 0 || len(m) < u || len(sig) < u { 10 | return false 11 | } 12 | 13 | pub0 := pub[0] 14 | 15 | for i := 1; i < u; i++ { 16 | if pub[i].Curve != pub0.Curve { 17 | return false 18 | } 19 | } 20 | 21 | for i := 0; i < u; i++ { 22 | if !VerifyBytes(pub[i], hashFunc, m[i], sig[i]) { 23 | return false 24 | } 25 | } 26 | 27 | return true 28 | } 29 | -------------------------------------------------------------------------------- /pubkey/bip0340/curves.go: -------------------------------------------------------------------------------- 1 | package bip0340 2 | 3 | import ( 4 | "encoding/asn1" 5 | "crypto/elliptic" 6 | ) 7 | 8 | type namedCurveInfo struct { 9 | namedCurve elliptic.Curve 10 | oid asn1.ObjectIdentifier 11 | } 12 | 13 | var namedCurves = make([]namedCurveInfo, 0) 14 | 15 | func AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) { 16 | namedCurves = append(namedCurves, namedCurveInfo{ 17 | namedCurve: curve, 18 | oid: oid, 19 | }) 20 | } 21 | 22 | func NamedCurveFromOid(oid asn1.ObjectIdentifier) elliptic.Curve { 23 | for i := range namedCurves { 24 | cur := &namedCurves[i] 25 | if cur.oid.Equal(oid) { 26 | return cur.namedCurve 27 | } 28 | } 29 | 30 | return nil 31 | } 32 | 33 | func OidFromNamedCurve(curve elliptic.Curve) (asn1.ObjectIdentifier, bool) { 34 | for i := range namedCurves { 35 | cur := &namedCurves[i] 36 | if cur.namedCurve == curve { 37 | return cur.oid, true 38 | } 39 | } 40 | 41 | return asn1.ObjectIdentifier{}, false 42 | } 43 | -------------------------------------------------------------------------------- /pubkey/bip0340/elliptic.go: -------------------------------------------------------------------------------- 1 | package bip0340 2 | 3 | var s256 *CurveParams 4 | 5 | func init() { 6 | s256 = &CurveParams{ 7 | Name: "secp256k1", 8 | BitSize: 256, 9 | P: bigFromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F"), 10 | N: bigFromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"), 11 | B: bigFromHex("0000000000000000000000000000000000000000000000000000000000000007"), 12 | Gx: bigFromHex("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"), 13 | Gy: bigFromHex("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8"), 14 | } 15 | } 16 | 17 | // The following conventions are used, with constants as defined for secp256k1. 18 | // We note that adapting this specification to other elliptic curves is not straightforward 19 | // and can result in an insecure scheme 20 | func S256() *CurveParams { 21 | return s256 22 | } 23 | -------------------------------------------------------------------------------- /pubkey/ecdsa/curves.go: -------------------------------------------------------------------------------- 1 | package ecdsa 2 | 3 | import ( 4 | "encoding/asn1" 5 | "crypto/elliptic" 6 | ) 7 | 8 | type namedCurveInfo struct { 9 | namedCurve elliptic.Curve 10 | oid asn1.ObjectIdentifier 11 | } 12 | 13 | var namedCurves = make([]namedCurveInfo, 0) 14 | 15 | func AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) { 16 | namedCurves = append(namedCurves, namedCurveInfo{ 17 | namedCurve: curve, 18 | oid: oid, 19 | }) 20 | } 21 | 22 | func NamedCurveFromOid(oid asn1.ObjectIdentifier) elliptic.Curve { 23 | for i := range namedCurves { 24 | cur := &namedCurves[i] 25 | if cur.oid.Equal(oid) { 26 | return cur.namedCurve 27 | } 28 | } 29 | 30 | return nil 31 | } 32 | 33 | func OidFromNamedCurve(curve elliptic.Curve) (asn1.ObjectIdentifier, bool) { 34 | for i := range namedCurves { 35 | cur := &namedCurves[i] 36 | if cur.namedCurve == curve { 37 | return cur.oid, true 38 | } 39 | } 40 | 41 | return asn1.ObjectIdentifier{}, false 42 | } 43 | -------------------------------------------------------------------------------- /pubkey/ecgdsa/curves.go: -------------------------------------------------------------------------------- 1 | package ecgdsa 2 | 3 | import ( 4 | "encoding/asn1" 5 | "crypto/elliptic" 6 | ) 7 | 8 | type namedCurveInfo struct { 9 | namedCurve elliptic.Curve 10 | oid asn1.ObjectIdentifier 11 | } 12 | 13 | var namedCurves = make([]namedCurveInfo, 0) 14 | 15 | func AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) { 16 | namedCurves = append(namedCurves, namedCurveInfo{ 17 | namedCurve: curve, 18 | oid: oid, 19 | }) 20 | } 21 | 22 | func NamedCurveFromOid(oid asn1.ObjectIdentifier) elliptic.Curve { 23 | for i := range namedCurves { 24 | cur := &namedCurves[i] 25 | if cur.oid.Equal(oid) { 26 | return cur.namedCurve 27 | } 28 | } 29 | 30 | return nil 31 | } 32 | 33 | func OidFromNamedCurve(curve elliptic.Curve) (asn1.ObjectIdentifier, bool) { 34 | for i := range namedCurves { 35 | cur := &namedCurves[i] 36 | if cur.namedCurve == curve { 37 | return cur.oid, true 38 | } 39 | } 40 | 41 | return asn1.ObjectIdentifier{}, false 42 | } 43 | -------------------------------------------------------------------------------- /pubkey/eckcdsa/curves.go: -------------------------------------------------------------------------------- 1 | package eckcdsa 2 | 3 | import ( 4 | "encoding/asn1" 5 | "crypto/elliptic" 6 | ) 7 | 8 | type namedCurveInfo struct { 9 | namedCurve elliptic.Curve 10 | oid asn1.ObjectIdentifier 11 | } 12 | 13 | var namedCurves = make([]namedCurveInfo, 0) 14 | 15 | func AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) { 16 | namedCurves = append(namedCurves, namedCurveInfo{ 17 | namedCurve: curve, 18 | oid: oid, 19 | }) 20 | } 21 | 22 | func NamedCurveFromOid(oid asn1.ObjectIdentifier) elliptic.Curve { 23 | for i := range namedCurves { 24 | cur := &namedCurves[i] 25 | if cur.oid.Equal(oid) { 26 | return cur.namedCurve 27 | } 28 | } 29 | 30 | return nil 31 | } 32 | 33 | func OidFromNamedCurve(curve elliptic.Curve) (asn1.ObjectIdentifier, bool) { 34 | for i := range namedCurves { 35 | cur := &namedCurves[i] 36 | if cur.namedCurve == curve { 37 | return cur.oid, true 38 | } 39 | } 40 | 41 | return asn1.ObjectIdentifier{}, false 42 | } 43 | -------------------------------------------------------------------------------- /pubkey/ecsdsa/curves.go: -------------------------------------------------------------------------------- 1 | package ecsdsa 2 | 3 | import ( 4 | "encoding/asn1" 5 | "crypto/elliptic" 6 | ) 7 | 8 | type namedCurveInfo struct { 9 | namedCurve elliptic.Curve 10 | oid asn1.ObjectIdentifier 11 | } 12 | 13 | var namedCurves = make([]namedCurveInfo, 0) 14 | 15 | func AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) { 16 | namedCurves = append(namedCurves, namedCurveInfo{ 17 | namedCurve: curve, 18 | oid: oid, 19 | }) 20 | } 21 | 22 | func NamedCurveFromOid(oid asn1.ObjectIdentifier) elliptic.Curve { 23 | for i := range namedCurves { 24 | cur := &namedCurves[i] 25 | if cur.oid.Equal(oid) { 26 | return cur.namedCurve 27 | } 28 | } 29 | 30 | return nil 31 | } 32 | 33 | func OidFromNamedCurve(curve elliptic.Curve) (asn1.ObjectIdentifier, bool) { 34 | for i := range namedCurves { 35 | cur := &namedCurves[i] 36 | if cur.namedCurve == curve { 37 | return cur.oid, true 38 | } 39 | } 40 | 41 | return asn1.ObjectIdentifier{}, false 42 | } 43 | -------------------------------------------------------------------------------- /pubkey/gost/utils.go: -------------------------------------------------------------------------------- 1 | package gost 2 | 3 | import ( 4 | "math/big" 5 | ) 6 | 7 | // Reverse bytes 8 | func Reverse(b []byte) []byte { 9 | d := make([]byte, len(b)) 10 | copy(d, b) 11 | 12 | for i, j := 0, len(d)-1; i < j; i, j = i+1, j-1 { 13 | d[i], d[j] = d[j], d[i] 14 | } 15 | 16 | return d 17 | } 18 | 19 | func bigIntFromBytes(b []byte) *big.Int { 20 | return new(big.Int).SetBytes(b) 21 | } 22 | -------------------------------------------------------------------------------- /pubkey/rsa/lower_safe.go: -------------------------------------------------------------------------------- 1 | package rsa 2 | 3 | import ( 4 | "errors" 5 | "math/big" 6 | "crypto/rsa" 7 | ) 8 | 9 | // rsa no padding encrypt 10 | func LowerSafeEncrypt(pub *rsa.PublicKey, msg []byte) ([]byte, error) { 11 | if pub == nil { 12 | return nil, errors.New("go-cryptobin/rsa: incorrect public key") 13 | } 14 | 15 | m := new(big.Int).SetBytes(msg) 16 | 17 | e := big.NewInt(int64(pub.E)) 18 | 19 | return new(big.Int).Exp(m, e, pub.N).Bytes(), nil 20 | } 21 | 22 | // rsa no padding decrypt 23 | func LowerSafeDecrypt(priv *rsa.PrivateKey, msg []byte) ([]byte, error) { 24 | if priv == nil { 25 | return nil, errors.New("go-cryptobin/rsa: incorrect private key") 26 | } 27 | 28 | c := new(big.Int).SetBytes(msg) 29 | 30 | return new(big.Int).Exp(c, priv.D, priv.N).Bytes(), nil 31 | } 32 | -------------------------------------------------------------------------------- /pubkey/rsa/lower_safe_test.go: -------------------------------------------------------------------------------- 1 | package rsa 2 | 3 | import ( 4 | "testing" 5 | 6 | cryptobin_test "github.com/deatil/go-cryptobin/tool/test" 7 | ) 8 | 9 | func Test_LowerSafeEncrypt(t *testing.T) { 10 | assertEqual := cryptobin_test.AssertEqualT(t) 11 | assertNoError := cryptobin_test.AssertNoErrorT(t) 12 | 13 | testPub := []byte(testPublicKeyCheck) 14 | pub, err := ParseXMLPublicKey(testPub) 15 | 16 | assertNoError(err, "Test_LowerSafeEncrypt-pub-Error") 17 | 18 | testPri := []byte(testPrivateKeyCheck) 19 | pri, err := ParseXMLPrivateKey(testPri) 20 | 21 | assertNoError(err, "Test_LowerSafeEncrypt-pri-Error") 22 | 23 | msg := []byte("test-test-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datadata") 24 | 25 | ct, err := LowerSafeEncrypt(pub, msg) 26 | assertNoError(err, "Test_LowerSafeEncrypt-en-Error") 27 | 28 | res, err := LowerSafeDecrypt(pri, ct) 29 | assertNoError(err, "Test_LowerSafeEncrypt-de-Error") 30 | 31 | assertEqual(string(res), string(msg), "Test_LowerSafeEncrypt") 32 | } 33 | -------------------------------------------------------------------------------- /pubkey/x25519/x25519_go1.20_test.go: -------------------------------------------------------------------------------- 1 | //go:build go1.20 2 | 3 | package x25519 4 | 5 | import ( 6 | "crypto/rand" 7 | "testing" 8 | ) 9 | 10 | func TestECDH(t *testing.T) { 11 | pub, priv, err := GenerateKey(rand.Reader) 12 | if err != nil { 13 | t.Fatal(err) 14 | } 15 | 16 | _, err = pub.ECDH() 17 | if err != nil { 18 | t.Fatal(err) 19 | } 20 | 21 | _, err = priv.ECDH() 22 | if err != nil { 23 | t.Fatal(err) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /rand/keygen/keygen.go: -------------------------------------------------------------------------------- 1 | package keygen 2 | 3 | import ( 4 | "io" 5 | ) 6 | 7 | type Keygen struct { 8 | length int 9 | reader io.Reader 10 | } 11 | 12 | func New(length int, reader io.Reader) *Keygen { 13 | return &Keygen{ 14 | length: length, 15 | reader: reader, 16 | } 17 | } 18 | 19 | func (this *Keygen) GenerateKey() ([]byte, error) { 20 | num := this.length / 8 21 | 22 | key := make([]byte, num) 23 | 24 | _, err := this.reader.Read(key) 25 | if err != nil { 26 | return nil, err 27 | } 28 | 29 | genKey := make([]byte, num) 30 | copy(genKey, key) 31 | 32 | for i := num; i < len(key); { 33 | for j := 0; j < num && i < len(key); j, i = j+1, i+1 { 34 | genKey[j] ^= key[i] 35 | } 36 | } 37 | 38 | return genKey, nil 39 | } 40 | -------------------------------------------------------------------------------- /rand/keygen/keygen_test.go: -------------------------------------------------------------------------------- 1 | package keygen 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | 7 | cryptobin_test "github.com/deatil/go-cryptobin/tool/test" 8 | ) 9 | 10 | func Test_Gen(t *testing.T) { 11 | assertEqual := cryptobin_test.AssertEqualT(t) 12 | assertNoError := cryptobin_test.AssertNoErrorT(t) 13 | assertNotEmpty := cryptobin_test.AssertNotEmptyT(t) 14 | 15 | data := bytes.NewBufferString("dfgtryertdfgdr345343rtyedfgtryertdfgdr345343rtye") 16 | res, err := New(128, data).GenerateKey() 17 | 18 | check := []byte("dfgtryertdfgdr34") 19 | 20 | assertNoError(err, "Gen") 21 | assertNotEmpty(res, "Gen") 22 | 23 | assertEqual(string(res), string(check), "Gen") 24 | } 25 | -------------------------------------------------------------------------------- /rand/prng/prng.go: -------------------------------------------------------------------------------- 1 | package prng 2 | 3 | import ( 4 | "hash" 5 | "errors" 6 | "crypto/md5" 7 | "crypto/sha1" 8 | ) 9 | 10 | var MD5PRNG = New(md5.New) 11 | var SHA1PRNG = New(sha1.New) 12 | 13 | type Prng struct { 14 | hash func() hash.Hash 15 | seed []byte 16 | } 17 | 18 | func New(hash func() hash.Hash) *Prng { 19 | return &Prng{ 20 | hash: hash, 21 | seed: make([]byte, 0), 22 | } 23 | } 24 | 25 | func NewWithSeed(hash func() hash.Hash, seed []byte) *Prng { 26 | return &Prng{ 27 | hash: hash, 28 | seed: seed, 29 | } 30 | } 31 | 32 | func (this *Prng) SetSeed(seed []byte) *Prng { 33 | this.seed = seed 34 | 35 | return this 36 | } 37 | 38 | func (this *Prng) Read(b []byte) (n int, err error) { 39 | hashs := this.makeSha(this.makeSha(this.seed)) 40 | 41 | n = len(b) 42 | if n > len(hashs) { 43 | return 0, errors.New("invalid length!") 44 | } 45 | 46 | copy(b, hashs) 47 | 48 | return n, nil 49 | } 50 | 51 | func (this *Prng) makeSha(data []byte) []byte { 52 | h := this.hash() 53 | h.Write(data) 54 | return h.Sum(nil) 55 | } 56 | -------------------------------------------------------------------------------- /rand/randomart/randomart_test.go: -------------------------------------------------------------------------------- 1 | package randomart 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func Test_Randomart(t *testing.T) { 8 | str := "123456789" 9 | 10 | res := Randomart(str) 11 | if len(res) == 0 { 12 | t.Error("Randomart error, got zero") 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /rand/zero/zero.go: -------------------------------------------------------------------------------- 1 | package zero 2 | 3 | import ( 4 | "io" 5 | ) 6 | 7 | // zero Reader 8 | type zeroReader struct { 9 | io.Reader 10 | } 11 | 12 | func (this *zeroReader) Read(dst []byte) (n int, err error) { 13 | for i := range dst { 14 | dst[i] = 0 15 | } 16 | 17 | return len(dst), nil 18 | } 19 | 20 | var Reader = &zeroReader{} 21 | -------------------------------------------------------------------------------- /rand/zero/zero_test.go: -------------------------------------------------------------------------------- 1 | package zero 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Reader(t *testing.T) { 9 | check := []byte{0, 0, 0, 0, 0, 0} 10 | str := make([]byte, 6) 11 | 12 | res, err := Reader.Read(str) 13 | if err != nil { 14 | t.Fatal(err) 15 | } 16 | 17 | if res != len(check) { 18 | t.Errorf("Read error, got %d, want %d", res, len(check)) 19 | } 20 | 21 | if fmt.Sprintf("%x", str) != fmt.Sprintf("%x", check) { 22 | t.Errorf("Read data error, got %x, want %x", str, check) 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /ssh/cipher.go: -------------------------------------------------------------------------------- 1 | package ssh 2 | -------------------------------------------------------------------------------- /ssh/kdf.go: -------------------------------------------------------------------------------- 1 | package ssh 2 | 3 | -------------------------------------------------------------------------------- /ssh/key.go: -------------------------------------------------------------------------------- 1 | package ssh 2 | 3 | import ( 4 | "errors" 5 | "reflect" 6 | ) 7 | 8 | // 反射获取结构体名称 9 | // Get Struct Name by reflect 10 | func GetStructName(s any) (name string) { 11 | p := reflect.TypeOf(s) 12 | 13 | if p.Kind() == reflect.Pointer { 14 | p = p.Elem() 15 | name = "*" 16 | } 17 | 18 | pkgPath := p.PkgPath() 19 | 20 | if pkgPath != "" { 21 | name += pkgPath + "." 22 | } 23 | 24 | return name + p.Name() 25 | } 26 | 27 | // check padding 28 | func checkOpenSSHKeyPadding(pad []byte) error { 29 | for i, b := range pad { 30 | if int(b) != i+1 { 31 | return errors.New("error decoding key: padding not as expected") 32 | } 33 | } 34 | 35 | return nil 36 | } 37 | -------------------------------------------------------------------------------- /tool/bytes/bytes_test.go: -------------------------------------------------------------------------------- 1 | package bytes 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/deatil/go-cryptobin/tool/test" 7 | ) 8 | 9 | func Test_SplitSize(t *testing.T) { 10 | assertEqual := test.AssertEqualT(t) 11 | 12 | data := SplitSize([]byte("1234567ytghyuj"), 5) 13 | check := [][]byte{ 14 | []byte("12345"), 15 | []byte("67ytg"), 16 | []byte("hyuj"), 17 | } 18 | 19 | assertEqual(data, check, "Test_SplitSize") 20 | } 21 | 22 | func Test_FromString(t *testing.T) { 23 | assertEqual := test.AssertEqualT(t) 24 | 25 | data := FromString("1234567ytghyuj") 26 | check := []byte("1234567ytghyuj") 27 | 28 | assertEqual(data, check, "Test_FromString") 29 | } 30 | 31 | func Test_ToString(t *testing.T) { 32 | assertEqual := test.AssertEqualT(t) 33 | 34 | data := ToString([]byte("1234567ytghyuj")) 35 | check := "1234567ytghyuj" 36 | 37 | assertEqual(data, check, "Test_ToString") 38 | } 39 | -------------------------------------------------------------------------------- /tool/cipher/cipher_test.go: -------------------------------------------------------------------------------- 1 | package cipher 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/deatil/go-cryptobin/tool/test" 7 | ) 8 | 9 | func Test_GetCipher(t *testing.T) { 10 | assertNotEmpty := test.AssertNotEmptyT(t) 11 | assertNoError := test.AssertNoErrorT(t) 12 | 13 | res, err := GetCipher("Aes") 14 | 15 | assertNoError(err, "Test_GetCipher") 16 | assertNotEmpty(res, "Test_GetCipher") 17 | } 18 | -------------------------------------------------------------------------------- /tool/error_event/error_event.go: -------------------------------------------------------------------------------- 1 | package error_event 2 | 3 | type ( 4 | // 错误方法 5 | ErrorFunc = func([]error) 6 | ) 7 | 8 | /** 9 | * 错误事件 10 | * 11 | * @create 2023-8-11 12 | * @author deatil 13 | */ 14 | type ErrorEvent struct { 15 | // 错误方法列表 16 | errorFuncs []ErrorFunc 17 | } 18 | 19 | // 构造函数 20 | func New() ErrorEvent { 21 | e := ErrorEvent{ 22 | errorFuncs: make([]ErrorFunc, 0), 23 | } 24 | 25 | return e 26 | } 27 | 28 | // 添加 29 | func (this ErrorEvent) On(fn ErrorFunc) ErrorEvent { 30 | this.errorFuncs = append(this.errorFuncs, fn) 31 | 32 | return this 33 | } 34 | 35 | // 执行 36 | func (this ErrorEvent) Trigger(errs []error) { 37 | if (len(this.errorFuncs) > 0) { 38 | for _, fn := range this.errorFuncs { 39 | fn(errs) 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /tool/errors/join.go: -------------------------------------------------------------------------------- 1 | package errors 2 | 3 | // Join concatenates the elements of err to create a new error. 4 | func Join(errs ...error) error { 5 | n := 0 6 | for _, err := range errs { 7 | if err != nil { 8 | n++ 9 | } 10 | } 11 | 12 | if n == 0 { 13 | return nil 14 | } 15 | 16 | e := &Errors{ 17 | errs: make([]error, 0, n), 18 | } 19 | 20 | for _, err := range errs { 21 | if err != nil { 22 | e.errs = append(e.errs, err) 23 | } 24 | } 25 | 26 | return e 27 | } 28 | -------------------------------------------------------------------------------- /tool/math/gcd/extended.go: -------------------------------------------------------------------------------- 1 | package gcd 2 | 3 | // Extended simple extended gcd 4 | func Extended(a, b int64) (int64, int64, int64) { 5 | if a == 0 { 6 | return b, 0, 1 7 | } 8 | gcd, xPrime, yPrime := Extended(b%a, a) 9 | return gcd, yPrime - (b/a)*xPrime, xPrime 10 | } 11 | -------------------------------------------------------------------------------- /tool/math/gcd/extended_test.go: -------------------------------------------------------------------------------- 1 | package gcd 2 | 3 | import "testing" 4 | 5 | func TestExtended(t *testing.T) { 6 | var testCasesExtended = []struct { 7 | name string 8 | a int64 9 | b int64 10 | gcd int64 11 | x int64 12 | y int64 13 | }{ 14 | {"gcd of 30 and 50", 30, 50, 10, 2, -1}, 15 | } 16 | for _, tc := range testCasesExtended { 17 | t.Run(tc.name, func(t *testing.T) { 18 | gcd, x, y := Extended(tc.a, tc.b) 19 | if gcd != tc.gcd && x != tc.x && y != tc.y { 20 | t.Fatalf("Expected values:\n\tGCD: Expected %v Returned %v,\n\tx: Expected %v Returned %v\n\ty: Expected %v Returned %v", tc.gcd, gcd, tc.x, x, tc.y, y) 21 | } 22 | }) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tool/math/gcd/extendedgcd.go: -------------------------------------------------------------------------------- 1 | package gcd 2 | 3 | // ExtendedRecursive finds and returns gcd(a, b), x, y satisfying a*x + b*y = gcd(a, b). 4 | func ExtendedRecursive(a, b int64) (int64, int64, int64) { 5 | if b > 0 { 6 | d, y, x := ExtendedRecursive(b, a%b) 7 | y -= (a / b) * x 8 | return d, x, y 9 | } 10 | 11 | return a, 1, 0 12 | } 13 | -------------------------------------------------------------------------------- /tool/math/gcd/extendedgcditerative.go: -------------------------------------------------------------------------------- 1 | package gcd 2 | 3 | // ExtendedIterative finds and returns gcd(a, b), x, y satisfying a*x + b*y = gcd(a, b). 4 | func ExtendedIterative(a, b int64) (int64, int64, int64) { 5 | var u, y, v, x int64 = 1, 1, 0, 0 6 | for a > 0 { 7 | var q int64 = b / a 8 | x, u = u, x-q*u 9 | y, v = v, y-q*v 10 | b, a = a, b-q*a 11 | } 12 | return b, x, y 13 | } 14 | -------------------------------------------------------------------------------- /tool/math/gcd/gcd.go: -------------------------------------------------------------------------------- 1 | package gcd 2 | 3 | // Recursive finds and returns the greatest common divisor of a given integer. 4 | func Recursive(a, b int64) int64 { 5 | if b == 0 { 6 | return a 7 | } 8 | return Recursive(b, a%b) 9 | } 10 | -------------------------------------------------------------------------------- /tool/math/gcd/gcditerative.go: -------------------------------------------------------------------------------- 1 | package gcd 2 | 3 | // Iterative Faster iterative version of GcdRecursive without holding up too much of the stack 4 | func Iterative(a, b int64) int64 { 5 | for b != 0 { 6 | a, b = b, a%b 7 | } 8 | return a 9 | } 10 | -------------------------------------------------------------------------------- /tool/math/lcm/lcm.go: -------------------------------------------------------------------------------- 1 | package lcm 2 | 3 | import ( 4 | "math" 5 | 6 | "github.com/deatil/go-cryptobin/tool/math/gcd" 7 | ) 8 | 9 | // Lcm returns the lcm of two numbers using the fact that lcm(a,b) * gcd(a,b) = | a * b | 10 | func Lcm(a, b int64) int64 { 11 | return int64(math.Abs(float64(a*b)) / float64(gcd.Iterative(a, b))) 12 | } 13 | -------------------------------------------------------------------------------- /tool/memory/aligned.go: -------------------------------------------------------------------------------- 1 | package memory 2 | 3 | import ( 4 | "reflect" 5 | "testing" 6 | ) 7 | 8 | func TestFieldIsAligned(t *testing.T, align uintptr, raw interface{}, fieldName string) { 9 | v := reflect.ValueOf(raw) 10 | for v.Kind() == reflect.Ptr { 11 | v = v.Elem() 12 | } 13 | vt := v.Type() 14 | 15 | vtf, ok := vt.FieldByName(fieldName) 16 | if !ok { 17 | t.Errorf("%s.%s not found", vt.Name(), fieldName) 18 | return 19 | } 20 | 21 | if vtf.Offset&(align-1) != 0 { 22 | t.Errorf("%s.%s is not Aligned. offset=%d", vt.Name(), fieldName, vtf.Offset) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tool/memory/convert.go: -------------------------------------------------------------------------------- 1 | package memory 2 | 3 | import ( 4 | "unsafe" 5 | ) 6 | 7 | func ConvertU32S(b []byte) []uint32 { 8 | sd := unsafe.SliceData(b) 9 | return unsafe.Slice((*uint32)(unsafe.Pointer(sd)), len(b)/4) 10 | } 11 | 12 | func P8(b []byte) *byte { 13 | sd := unsafe.SliceData(b) 14 | return sd 15 | } 16 | -------------------------------------------------------------------------------- /tool/memory/memset.go: -------------------------------------------------------------------------------- 1 | package memory 2 | 3 | func Memset(a []byte, v byte) { 4 | if len(a) == 0 { 5 | return 6 | } 7 | a[0] = v 8 | for bp := 1; bp < len(a); bp *= 2 { 9 | copy(a[bp:], a[:bp]) 10 | } 11 | } 12 | 13 | func MemsetU32(a []uint32, v uint32) { 14 | if len(a) == 0 { 15 | return 16 | } 17 | a[0] = v 18 | for bp := 1; bp < len(a); bp *= 2 { 19 | copy(a[bp:], a[:bp]) 20 | } 21 | } 22 | 23 | func MemsetU64(a []uint64, v uint64) { 24 | if len(a) == 0 { 25 | return 26 | } 27 | a[0] = v 28 | for bp := 1; bp < len(a); bp *= 2 { 29 | copy(a[bp:], a[:bp]) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tool/memory/ptr.go: -------------------------------------------------------------------------------- 1 | package memory 2 | 3 | import ( 4 | "unsafe" 5 | ) 6 | 7 | // void pointer(byte b) 8 | // if b is nil, panic 9 | func VP8(b []byte) unsafe.Pointer { 10 | return unsafe.Pointer(&b[0]) 11 | } 12 | 13 | // void pointer(uint32 b) 14 | // if b is nil, panic 15 | func VPU32(b []uint32) unsafe.Pointer { 16 | return unsafe.Pointer(&b[0]) 17 | } 18 | 19 | // void pointer(uint64 b) 20 | // if b is nil, panic 21 | func VPU64(b []uint64) unsafe.Pointer { 22 | return unsafe.Pointer(&b[0]) 23 | } 24 | -------------------------------------------------------------------------------- /tool/recover/recover.go: -------------------------------------------------------------------------------- 1 | package recover 2 | 3 | import ( 4 | "fmt" 5 | "errors" 6 | ) 7 | 8 | // recover and return error when throw panic 9 | func Recover(fn func()) (err error) { 10 | defer func() { 11 | if e := recover(); e != nil { 12 | err = errors.New(fmt.Sprintf("%v", e)) 13 | } 14 | }() 15 | 16 | fn() 17 | 18 | return 19 | } 20 | -------------------------------------------------------------------------------- /tool/recover/recover_test.go: -------------------------------------------------------------------------------- 1 | package recover 2 | 3 | import ( 4 | "errors" 5 | "testing" 6 | 7 | "github.com/deatil/go-cryptobin/tool/test" 8 | ) 9 | 10 | func Test_Recover(t *testing.T) { 11 | assertEqual := test.AssertEqualT(t) 12 | assertError := test.AssertErrorT(t) 13 | 14 | err := Recover(func() { 15 | panic("test panic") 16 | }) 17 | 18 | assertError(err, "Test_Recover-assertError") 19 | assertEqual(err, errors.New("test panic"), "Test_Recover-assertEqual") 20 | } 21 | -------------------------------------------------------------------------------- /tool/utils/utils.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "crypto/rand" 5 | ) 6 | 7 | // 随机生成字符 8 | func GenRandom(n int) ([]byte, error) { 9 | value := make([]byte, n) 10 | _, err := rand.Read(value) 11 | return value, err 12 | } 13 | -------------------------------------------------------------------------------- /tool/utils/utils_test.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/deatil/go-cryptobin/tool/test" 7 | ) 8 | 9 | func Test_GenRandom(t *testing.T) { 10 | assertEqual := test.AssertEqualT(t) 11 | assertNotEmpty := test.AssertNotEmptyT(t) 12 | 13 | data, _ := GenRandom(12) 14 | assertNotEmpty(data, "Test_GenRandom") 15 | 16 | assertEqual(len(data), 12, "Test_GenRandom") 17 | } 18 | -------------------------------------------------------------------------------- /tool/xor/xor_amd64.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | //go:build amd64 && !generic 5 | // +build amd64,!generic 6 | 7 | package xor 8 | 9 | // XorBytes xors the bytes in a and b. The destination should have enough 10 | // space, otherwise xorBytes will panic. Returns the number of bytes xor'd. 11 | func XorBytes(dst, a, b []byte) int { 12 | n := len(a) 13 | if len(b) < n { 14 | n = len(b) 15 | } 16 | if n == 0 { 17 | return 0 18 | } 19 | _ = dst[n-1] 20 | xorBytesSSE2(&dst[0], &a[0], &b[0], n) // amd64 must have SSE2 21 | return n 22 | } 23 | 24 | func XorWords(dst, a, b []byte) { 25 | XorBytes(dst, a, b) 26 | } 27 | 28 | //go:noescape 29 | func xorBytesSSE2(dst, a, b *byte, n int) 30 | -------------------------------------------------------------------------------- /tool/xor/xor_arm64.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | //go:build arm64 && !generic 5 | // +build arm64,!generic 6 | 7 | package xor 8 | 9 | // xorBytes xors the bytes in a and b. The destination should have enough 10 | // space, otherwise xorBytes will panic. Returns the number of bytes xor'd. 11 | func XorBytes(dst, a, b []byte) int { 12 | n := len(a) 13 | if len(b) < n { 14 | n = len(b) 15 | } 16 | if n == 0 { 17 | return 0 18 | } 19 | // make sure dst has enough space 20 | _ = dst[n-1] 21 | 22 | xorBytesARM64(&dst[0], &a[0], &b[0], n) 23 | return n 24 | } 25 | 26 | func XorWords(dst, a, b []byte) { 27 | XorBytes(dst, a, b) 28 | } 29 | 30 | //go:noescape 31 | func xorBytesARM64(dst, a, b *byte, n int) 32 | -------------------------------------------------------------------------------- /x509/boring.go: -------------------------------------------------------------------------------- 1 | //go:build boringcrypto 2 | 3 | package x509 4 | 5 | import ( 6 | "crypto/rsa" 7 | "crypto/ecdsa" 8 | "crypto/elliptic" 9 | 10 | "github.com/deatil/go-cryptobin/x509/fipstls" 11 | ) 12 | 13 | // boringAllowCert reports whether c is allowed to be used 14 | // in a certificate chain by the current fipstls enforcement setting. 15 | // It is called for each leaf, intermediate, and root certificate. 16 | func boringAllowCert(c *Certificate) bool { 17 | if !fipstls.Required() { 18 | return true 19 | } 20 | 21 | // The key must be RSA 2048, RSA 3072, RSA 4096, 22 | // or ECDSA P-256, P-384, P-521. 23 | switch k := c.PublicKey.(type) { 24 | default: 25 | return false 26 | case *rsa.PublicKey: 27 | if size := k.N.BitLen(); size != 2048 && size != 3072 && size != 4096 { 28 | return false 29 | } 30 | case *ecdsa.PublicKey: 31 | if k.Curve != elliptic.P256() && k.Curve != elliptic.P384() && k.Curve != elliptic.P521() { 32 | return false 33 | } 34 | } 35 | 36 | return true 37 | } 38 | -------------------------------------------------------------------------------- /x509/fipstls/stub.s: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | //go:build boringcrypto 6 | 7 | // runtime_arg0 is declared in tls.go without a body. 8 | // It's provided by package runtime, 9 | // but the go command doesn't know that. 10 | // Having this assembly file keeps the go command 11 | // from complaining about the missing body 12 | // (because the implementation might be here). 13 | -------------------------------------------------------------------------------- /x509/notboring.go: -------------------------------------------------------------------------------- 1 | //go:build !boringcrypto 2 | 3 | package x509 4 | 5 | func boringAllowCert(c *Certificate) bool { return true } 6 | --------------------------------------------------------------------------------