package cryptoimport (// #nosec G505)// RSAEncryptHashed encrypts given data with RSA, prefixing with a hash.func ( []byte, *rsa.PublicKey, io.Reader) ([]byte, error) {// Preparing `data_with_hash`. // data_with_hash := SHA1(data) + data + (any random bytes); // such that the length equals 255 bytes;var [rsaWithHashLen]byteiflen() > rsaDataLen {returnnil, errors.Errorf("data length %d is too big", len()) }// Filling data_with_hash with random bytes.if , := io.ReadFull(, [:]); != nil {returnnil, } := sha1.Sum() // #nosec// Replacing first 20 bytes with sha1(data).copy([:sha1.Size], [:])// Replacing other bytes with data itself.copy([sha1.Size:], )// Encrypting "dataWithHash" with RSA. := rsaEncrypt([:], )return , nil}// RSADecryptHashed decrypts given data with RSA.func ( []byte, *rsa.PrivateKey) ([]byte, error) {var [rsaWithHashLen]byteif !rsaDecrypt(, , [:]) {returnnil, errors.New("invalid data_with_hash") } := [:sha1.Size] := [sha1.Size:]// Guessing such data that sha1(data) == hash.for := 0; <= len(); ++ { := [:len()-] := sha1.Sum() // #nosecifbytes.Equal([:], ) {// Found.return , nil } }// This can be caused by invalid keys or implementation bug.returnnil, errors.New("hash mismatch")}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.