crypto

package
v1.2.30 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ISO78164Padding = ISO9791Method2Padding

ISO/IEC 7816-4:2005[9] is identical to the bit padding scheme, applied to a plain text of N bytes. This means in practice that the first byte is a mandatory byte valued '80' (Hexadecimal) followed, if needed, by 0 to N − 1 bytes set to '00', until the end of the block is reached. ISO/IEC 7816-4 itself is a communication standard for smart cards containing a file system, and in itself does not contain any cryptographic specifications.

View Source
var ISO78164UnPadding = ISO9791Method2UnPadding

See https://en.wikipedia.org/wiki/Data_source_name

View Source
var ZeroUnPadding = ISO9791Method1UnPadding

Functions

func ANSIX923Padding

func ANSIX923Padding(s []byte, block int) []byte

In ANSI X9.23, between 1 and 8 bytes are always added as padding. The block is padded with random bytes (although many implementations use 00) and the last byte of the block is set to the number of bytes added.[6] Example: In the following example the block size is 8 bytes, and padding is required for 4 bytes (in hexadecimal format) ... | DD DD DD DD DD DD DD DD | DD DD DD DD 00 00 00 04 | See https://en.wikipedia.org/wiki/Padding_(cryptography)#ANSI_X9.23

Example
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/searKing/golang/go/crypto"
)

var unpad = []byte{0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD}

const blockSizeInByte = 8

func main() {
	padded := crypto.ANSIX923Padding(unpad, blockSizeInByte)
	fmt.Println("padding:")
	fmt.Print(hex.Dump(padded))
	decoded, err := crypto.ANSIX923UnPadding(padded, blockSizeInByte)
	if err != nil {
		fmt.Printf("err: %s", err)
		return
	}
	fmt.Println("unpadding:")
	fmt.Print(hex.Dump(decoded))

}
Output:

padding:
00000000  dd dd dd dd dd dd dd dd  dd dd dd dd 00 00 00 04  |................|
unpadding:
00000000  dd dd dd dd dd dd dd dd  dd dd dd dd              |............|

func ANSIX923UnPadding

func ANSIX923UnPadding(s []byte, block int) ([]byte, error)

func IOS10126Padding

func IOS10126Padding(s []byte, block int) []byte

ISO 10126 (withdrawn, 2007[7][8]) specifies that the padding should be done at the end of that last block with random bytes, and the padding boundary should be specified by the last byte. Example: In the following example the block size is 8 bytes and padding is required for 4 bytes ... | DD DD DD DD DD DD DD DD | DD DD DD DD 81 A6 23 04 | See https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO_10126

Example
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/searKing/golang/go/crypto"
)

var unpad = []byte{0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD}

const blockSizeInByte = 8

func main() {
	padded := crypto.IOS10126Padding(unpad, blockSizeInByte)
	for i := len(unpad); i < len(padded)-1; i++ {
		padded[i] = 0
	}
	fmt.Println("padding, replace random with 0x00:")
	fmt.Print(hex.Dump(padded))
	decoded, err := crypto.IOS10126UnPadding(padded, blockSizeInByte)
	if err != nil {
		fmt.Printf("err: %s", err)
		return
	}
	fmt.Println("unpadding:")
	fmt.Print(hex.Dump(decoded))

}
Output:

padding, replace random with 0x00:
00000000  dd dd dd dd dd dd dd dd  dd dd dd dd 00 00 00 04  |................|
unpadding:
00000000  dd dd dd dd dd dd dd dd  dd dd dd dd              |............|

func IOS10126UnPadding

func IOS10126UnPadding(s []byte, block int) ([]byte, error)

func ISO9791Method1Padding

func ISO9791Method1Padding(s []byte, block int) []byte

the data string D to be input to the MAC algorithm shall be right-padded with as few (possible none) '0' bits as necessary to obtain a data string whose length(in bits) is a positive integer multiple of n. NOTE 1 MAC algorithm using Padding Method 1 may be subject to trivial forgery attacks. 2 If the data string is empty, Padding Method1 specifies that it is right-padded with n '0' bits. See https://en.wikipedia.org/wiki/ISO/IEC_9797-1#Padding_method_1

Example
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/searKing/golang/go/crypto"
)

var unpad = []byte{0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD}

const blockSizeInByte = 8

func main() {
	padded := crypto.ISO9791Method1Padding(unpad, blockSizeInByte)
	fmt.Println("padding:")
	fmt.Print(hex.Dump(padded))
	decoded := crypto.ISO9791Method1UnPadding(padded, blockSizeInByte)
	fmt.Println("unpadding:")
	fmt.Print(hex.Dump(decoded))

}
Output:

padding:
00000000  dd dd dd dd dd dd dd dd  dd dd dd dd 00 00 00 00  |................|
unpadding:
00000000  dd dd dd dd dd dd dd dd  dd dd dd dd              |............|

func ISO9791Method1UnPadding

func ISO9791Method1UnPadding(s []byte, block int) []byte

func ISO9791Method2Padding

func ISO9791Method2Padding(s []byte, block int) []byte

the data string D to be input to the MAC algorithm shall be right-padded with a single '1' bit. The resulting string shall then be right-padded with as few (possible none) '0' bits as necessary to obtain a data string whose length(in bits) is a positive integer multiple of n. See https://en.wikipedia.org/wiki/ISO/IEC_9797-1#Padding_method_2

Example
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/searKing/golang/go/crypto"
)

var unpad = []byte{0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD}

const blockSizeInByte = 8

func main() {
	padded := crypto.ISO9791Method2Padding(unpad, blockSizeInByte)
	fmt.Println("padding:")
	fmt.Print(hex.Dump(padded))
	decoded, err := crypto.ISO9791Method2UnPadding(padded, blockSizeInByte)
	if err != nil {
		fmt.Printf("err: %s", err)
		return
	}
	fmt.Println("unpadding:")
	fmt.Print(hex.Dump(decoded))

}
Output:

padding:
00000000  dd dd dd dd dd dd dd dd  dd dd dd dd 80 00 00 00  |................|
unpadding:
00000000  dd dd dd dd dd dd dd dd  dd dd dd dd              |............|

func ISO9791Method2UnPadding

func ISO9791Method2UnPadding(s []byte, block int) ([]byte, error)

func ISO9791Method3Padding

func ISO9791Method3Padding(s []byte, block int) []byte

the data string D to be input to the MAC algorithm shall be right-padded with as few (possible none) '0' bits as necessary to obtain a data string whose length(in bits) is a positive integer multiple of n. The resulting string shall then be left-padded with a block L. The block L consists of the binary representation of the length(in bits) Ld of the unpadded data string D, left-padded with as few (possible none) '0' bits as necessary to obtain an n-bit block. The right-most bit of the block L corresponds to the least significant bit of the binary representation of Ld. NOTE 1 Padding Method 3 is not suitable for use in situations where the length of the data string is not available prior to the start of the MAC calculation. See https://en.wikipedia.org/wiki/ISO/IEC_9797-1#Padding_method_2

Example
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/searKing/golang/go/crypto"
)

var unpad = []byte{0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD}

const blockSizeInByte = 8

func main() {
	padded := crypto.ISO9791Method3Padding(unpad, blockSizeInByte)
	fmt.Println("padding:")
	fmt.Print(hex.Dump(padded))
	decoded, err := crypto.ISO9791Method3UnPadding(padded, blockSizeInByte)
	if err != nil {
		fmt.Printf("err: %s", err)
		return
	}
	fmt.Println("unpadding:")
	fmt.Print(hex.Dump(decoded))

}
Output:

padding:
00000000  00 00 00 00 00 00 00 60  dd dd dd dd dd dd dd dd  |.......`........|
00000010  dd dd dd dd 00 00 00 00                           |........|
unpadding:
00000000  dd dd dd dd dd dd dd dd  dd dd dd dd              |............|

func ISO9791Method3UnPadding

func ISO9791Method3UnPadding(s []byte, block int) ([]byte, error)

func PKCS5Padding

func PKCS5Padding(s []byte) []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.

Example
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/searKing/golang/go/crypto"
)

var unpad = []byte{0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD}

func main() {
	padded := crypto.PKCS5Padding(unpad)
	fmt.Println("padding:")
	fmt.Print(hex.Dump(padded))
	decoded, err := crypto.PKCS5UnPadding(padded)
	if err != nil {
		fmt.Printf("err: %s", err)
		return
	}
	fmt.Println("unpadding:")
	fmt.Print(hex.Dump(decoded))

}
Output:

padding:
00000000  dd dd dd dd dd dd dd dd  dd dd dd dd 04 04 04 04  |................|
unpadding:
00000000  dd dd dd dd dd dd dd dd  dd dd dd dd              |............|

func PKCS5UnPadding

func PKCS5UnPadding(s []byte) ([]byte, error)

func PKCS7Padding

func PKCS7Padding(s []byte, block int) []byte

Padding is in whole bytes. The value of each added byte is the number of bytes that are added, i.e. N bytes, each of value N are added. The number of bytes added will depend on the block boundary to which the message needs to be extended. The padding will be one of: 01 02 02 03 03 03 04 04 04 04 05 05 05 05 05 06 06 06 06 06 06 etc. This padding method (as well as the previous two) is well-defined if and only if N is less than 256. Example: In the following example the block size is 8 bytes and padding is required for 4 bytes ... | DD DD DD DD DD DD DD DD | DD DD DD DD 04 04 04 04 | If the length of the original data is an integer multiple of the block size B, then an extra block of bytes with value B is added. This is necessary so the deciphering algorithm can determine with certainty whether the last byte of the last block is a pad byte indicating the number of padding bytes added or part of the plaintext message. Consider a plaintext message that is an integer multiple of B bytes with the last byte of plaintext being 01. With no additional information, the deciphering algorithm will not be able to determine whether the last byte is a plaintext byte or a pad byte. However, by adding B bytes each of value B after the 01 plaintext byte, the deciphering algorithm can always treat the last byte as a pad byte and strip the appropriate number of pad bytes off the end of the ciphertext; said number of bytes to be stripped based on the value of the last byte. See https://tools.ietf.org/html/rfc5652#section-6.3

Example
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/searKing/golang/go/crypto"
)

var unpad = []byte{0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD}

const blockSizeInByte = 8

func main() {
	padded := crypto.PKCS7Padding(unpad, blockSizeInByte)
	fmt.Println("padding:")
	fmt.Print(hex.Dump(padded))
	decoded, err := crypto.PKCS7UnPadding(padded, blockSizeInByte)
	if err != nil {
		fmt.Printf("err: %s", err)
		return
	}
	fmt.Println("unpadding:")
	fmt.Print(hex.Dump(decoded))

}
Output:

padding:
00000000  dd dd dd dd dd dd dd dd  dd dd dd dd 04 04 04 04  |................|
unpadding:
00000000  dd dd dd dd dd dd dd dd  dd dd dd dd              |............|

func PKCS7UnPadding

func PKCS7UnPadding(s []byte, block int) ([]byte, error)

Types

This section is empty.

Directories

Path Synopsis
https://www.oauth.com/oauth2-servers/client-registration/client-id-secret/
https://www.oauth.com/oauth2-servers/client-registration/client-id-secret/
package rand_ Creating Random Strings in Go
package rand_ Creating Random Strings in Go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL