-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
123 lines (111 loc) · 2.64 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package m3u8
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"fmt"
"os"
"os/exec"
)
// ============================== shell相关 ==============================
// 判断文件是否存在
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// 执行 shell
func execUnixShell(s string) {
cmd := exec.Command("/bin/bash", "-c", s)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
panic(err)
}
fmt.Printf("%s", out.String())
}
func execWinShell(s string) error {
cmd := exec.Command("cmd", "/C", s)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return err
}
fmt.Printf("%s", out.String())
return nil
}
// windows 合并文件
func win_merge_file(path string) {
os.Chdir(path)
execWinShell("copy /b *.ts merge.tmp")
execWinShell("del /Q *.ts")
os.Rename("merge.tmp", "merge.mp4")
}
// unix 合并文件
func unix_merge_file(path string) {
os.Chdir(path)
//cmd := `ls *.ts |sort -t "\." -k 1 -n |awk '{print $0}' |xargs -n 1 -I {} bash -c "cat {} >> new.tmp"`
cmd := `cat *.ts >> merge.tmp`
execUnixShell(cmd)
execUnixShell("rm -rf *.ts")
os.Rename("merge.tmp", "merge.mp4")
}
// ============================== 加解密相关 ==============================
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS7UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
func AesEncrypt(origData, key []byte, ivs ...[]byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
var iv []byte
if len(ivs) == 0 {
iv = key
} else {
iv = ivs[0]
}
origData = PKCS7Padding(origData, blockSize)
blockMode := cipher.NewCBCEncrypter(block, iv[:blockSize])
crypted := make([]byte, len(origData))
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
}
func AesDecrypt(crypted, key []byte, ivs ...[]byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
var iv []byte
if len(ivs) == 0 {
iv = key
} else {
iv = ivs[0]
}
blockMode := cipher.NewCBCDecrypter(block, iv[:blockSize])
origData := make([]byte, len(crypted))
blockMode.CryptBlocks(origData, crypted)
origData = PKCS7UnPadding(origData)
return origData, nil
}
func checkErr(e error) {
if e != nil {
logger.Panic(e)
}
}