MCPcopy Create free account
hub / github.com/chanify/chanify / Decrypt

Method Decrypt

crypto/crypto.go:150–175  ·  view source on GitHub ↗

Decrypt data with sercet key

(data []byte)

Source from the content-addressed store, hash-verified

148
149// Decrypt data with sercet key
150func (k *SecretKey) Decrypt(data []byte) ([]byte, error) {
151 if len(data) == 0 {
152 return nil, ErrInvalidMessage
153 }
154 var rLen int
155 switch data[0] {
156 case 2, 3, 4:
157 rLen = (k.PublicKey.Curve.Params().BitSize + 7) / 4
158 default:
159 return nil, ErrInvalidKey
160 }
161
162 R := ecdsa.PublicKey{}
163 R.Curve = elliptic.P256()
164 R.X, R.Y = elliptic.Unmarshal(R.Curve, data[:rLen])
165
166 Z, _ := calcSharedKey(&k.PrivateKey, &R)
167
168 X := data[:rLen]
169 K := x963KDF(Z, X, sha256.New)
170 key := K[:eciesKeyLen]
171 nonce := K[eciesKeyLen:]
172 block, _ := aes.NewCipher(key)
173 aesgcm, _ := cipher.NewGCMWithNonceSize(block, eciesKeyLen)
174 return aesgcm.Open(nil, nonce, data[rLen:], nil)
175}
176
177func marshalPublicKey(key *ecdsa.PublicKey) []byte {
178 return elliptic.Marshal(key.Curve, key.X, key.Y)

Callers 3

TestCryptoFunction · 0.45
TestKeyFailedFunction · 0.45
bindBodyJSONMethod · 0.45

Calls 2

calcSharedKeyFunction · 0.85
x963KDFFunction · 0.85

Tested by 2

TestCryptoFunction · 0.36
TestKeyFailedFunction · 0.36