Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: backwardn/encrypt
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: lib-design/crypto
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 9 commits
  • 20 files changed
  • 1 contributor

Commits on Nov 23, 2020

  1. Create FUNDING.yml

    txthinking authored Nov 23, 2020

    Verified

    This commit was signed with the committer’s verified signature.
    Robbepop Robin Freyler
    Copy the full SHA
    be062b2 View commit details

Commits on Jan 21, 2021

  1. RSASignWithSHA256PKCS1

    txthinking committed Jan 21, 2021

    Verified

    This commit was signed with the committer’s verified signature.
    Robbepop Robin Freyler
    Copy the full SHA
    5ab4dd5 View commit details

Commits on Mar 26, 2021

  1. Update README.md

    txthinking authored Mar 26, 2021

    Verified

    This commit was signed with the committer’s verified signature.
    Robbepop Robin Freyler
    Copy the full SHA
    12488ec View commit details

Commits on Jun 8, 2021

  1. Update README.md

    txthinking authored Jun 8, 2021

    Verified

    This commit was signed with the committer’s verified signature.
    Robbepop Robin Freyler
    Copy the full SHA
    a0c8a0e View commit details

Commits on Jun 24, 2021

  1. go mod

    txthinking committed Jun 24, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    0b23002 View commit details
  2. Update README.md

    txthinking authored Jun 24, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    eb6f2bf View commit details
  3. Update README.md

    txthinking authored Jun 24, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    d76c5ee View commit details

Commits on Jul 16, 2021

  1. Update FUNDING.yml

    txthinking authored Jul 16, 2021

    Verified

    This commit was signed with the committer’s verified signature.
    Robbepop Robin Freyler
    Copy the full SHA
    de9624a View commit details

Commits on Jan 23, 2022

  1. Update README.md

    txthinking authored Jan 23, 2022

    Verified

    This commit was signed with the committer’s verified signature.
    Robbepop Robin Freyler
    Copy the full SHA
    2590505 View commit details
Showing with 83 additions and 28 deletions.
  1. +12 −0 .github/FUNDING.yml
  2. +11 −5 README.md
  3. +5 −6 aes.go
  4. +3 −3 aes_test.go
  5. +1 −1 encoding.go
  6. +1 −1 encoding_test.go
  7. +8 −0 go.mod
  8. +10 −0 go.sum
  9. +1 −1 hkdf.go
  10. +1 −1 hkdf_test.go
  11. +1 −1 hmac.go
  12. +1 −1 hmac_test.go
  13. +1 −1 kv.go
  14. +1 −1 kv_test.go
  15. +1 −1 md5.go
  16. +1 −1 md5_test.go
  17. +21 −1 rsa.go
  18. +1 −1 sha.go
  19. +1 −1 sha_test.go
  20. +1 −1 uri.go
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# These are supported funding model platforms

github: txthinking
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom:
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
## Encrypt
## crypto

[![GoDoc](https://godoc.org/github.com/txthinking/encrypt?status.svg)](https://godoc.org/github.com/txthinking/encrypt)
[![GoDoc](https://godoc.org/github.com/txthinking/crypto?status.svg)](https://godoc.org/github.com/txthinking/crypto)

[🗣 Talks](https://t.me/txthinking_talks)
[💬 Join](https://join.txthinking.com)
[🩸 Youtube](https://www.youtube.com/txthinking)
[❤️ Sponsor](https://github.com/sponsors/txthinking)

Encryption, Hash, Encoding Library.

❤️ A project by [txthinking.com](https://www.txthinking.com)

### Install

```
$ go get github.com/txthinking/encrypt
$ go get github.com/txthinking/crypto
```

### Features
@@ -27,7 +34,6 @@ $ go get github.com/txthinking/encrypt
* RFC3986 URI Encoding
* RSA SHA256

License
---
## License

Licensed under The MIT License
11 changes: 5 additions & 6 deletions aes.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import (
"bytes"
@@ -120,16 +120,15 @@ func AESCBCDecrypt(c, k []byte) ([]byte, error) {
return cb, nil
}

// PKCS5Padding can append data of PKCS5
// Common blockSize is aes.BlockSize
func PKCS5Padding(c []byte, blockSize int) []byte {
// PKCS#5 padding is identical to PKCS#7 padding, except that it has only been defined for block ciphers that use a 64-bit (8-byte) block size. In practice the two can be used interchangeably.
// Common blockSize is aes.BlockSize.
func PKCSPadding(c []byte, blockSize int) []byte {
pl := blockSize - len(c)%blockSize
p := bytes.Repeat([]byte{byte(pl)}, pl)
return append(c, p...)
}

// PKCS5UnPadding can unappend data of PKCS5
func PKCS5UnPadding(s []byte) ([]byte, error) {
func PKCSUnPadding(s []byte) ([]byte, error) {
l := len(s)
if l == 0 {
return nil, errors.New("s too short")
6 changes: 3 additions & 3 deletions aes_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import (
"bytes"
@@ -40,7 +40,7 @@ func TestAESCBC(t *testing.T) {
s := []byte("txthinking")
t.Log("input:", hex.EncodeToString(s), len(s))

sp := PKCS5Padding(s, 16)
sp := PKCSPadding(s, 16)
r, err := AESCBCEncrypt(sp, KEY)
if err != nil {
t.Fatal(err)
@@ -51,7 +51,7 @@ func TestAESCBC(t *testing.T) {
if err != nil {
t.Fatal(err)
}
r, err = PKCS5UnPadding(r)
r, err = PKCSUnPadding(r)
if err != nil {
t.Fatal(err)
}
2 changes: 1 addition & 1 deletion encoding.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import (
"bytes"
2 changes: 1 addition & 1 deletion encoding_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import (
"testing"
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/txthinking/crypto

go 1.16

require (
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e
golang.org/x/text v0.3.6
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
2 changes: 1 addition & 1 deletion hkdf.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import (
"crypto/rand"
2 changes: 1 addition & 1 deletion hkdf_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import (
"encoding/hex"
2 changes: 1 addition & 1 deletion hmac.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import (
"crypto/hmac"
2 changes: 1 addition & 1 deletion hmac_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import (
"encoding/hex"
2 changes: 1 addition & 1 deletion kv.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import (
"encoding/hex"
2 changes: 1 addition & 1 deletion kv_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import "testing"

2 changes: 1 addition & 1 deletion md5.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import (
"crypto/md5"
2 changes: 1 addition & 1 deletion md5_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import (
"testing"
22 changes: 21 additions & 1 deletion rsa.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import (
"crypto"
@@ -48,3 +48,23 @@ func RSAVerifyWithSHA256(data, sign, key []byte) error {
}
return nil
}

// key is private key, PKCS#1.
func RSASignWithSHA256PKCS1(data []byte, key []byte) ([]byte, error) {
h := sha256.New()
h.Write(data)
b := h.Sum(nil)
block, _ := pem.Decode(key)
if block == nil {
return nil, errors.New("invalid key")
}
k, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
b, err = rsa.SignPKCS1v15(rand.Reader, k, crypto.SHA256, b)
if err != nil {
return nil, err
}
return b, nil
}
2 changes: 1 addition & 1 deletion sha.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import (
"crypto/sha1"
2 changes: 1 addition & 1 deletion sha_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import (
"testing"
2 changes: 1 addition & 1 deletion uri.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package encrypt
package crypto

import (
"net/url"