xxtea.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "bytes"
  4. "os"
  5. "strings"
  6. "sync"
  7. "github.com/xxtea/xxtea-go/xxtea"
  8. )
  9. func EncodeFile(srcFile string, destFile string, password string, sign string, sufixes []string) bool {
  10. //fmt.Println("EncodeFile " + srcFile)
  11. // 是否要加密的文件
  12. if isNeedEncode(srcFile, sufixes) {
  13. // 打开文件
  14. dat, err := os.ReadFile(srcFile)
  15. if err != nil {
  16. return false
  17. }
  18. // 已经加密过
  19. if strings.HasPrefix(string(dat), sign) {
  20. return false
  21. }
  22. encrypt_data := xxtea.Encrypt([]byte(dat), []byte(password))
  23. s := make([][]byte, 2)
  24. s[0] = []byte(sign)
  25. s[1] = encrypt_data
  26. sep := []byte("")
  27. os.WriteFile(destFile, bytes.Join(s, sep), 0777)
  28. return true
  29. }
  30. return false
  31. }
  32. func DecodeFile(srcFile string, destFile string, password string, sign string, sufixes []string) bool {
  33. // 是否要加密的文件
  34. if isNeedEncode(srcFile, sufixes) {
  35. // 打开文件
  36. dat, err := os.ReadFile(srcFile)
  37. if err != nil {
  38. return false
  39. }
  40. // 没有加密过
  41. if !strings.HasPrefix(string(dat), sign) {
  42. return false
  43. }
  44. decrypt_data := xxtea.Decrypt(([]byte(dat))[len(sign):], []byte(password))
  45. os.WriteFile(destFile, decrypt_data, 0777)
  46. return true
  47. }
  48. return false
  49. }
  50. func DoEncodeFile(srcFile string, destFile string, password string, sign string, wg *sync.WaitGroup, sufixes []string) {
  51. defer wg.Done()
  52. if !EncodeFile(srcFile, destFile, password, sign, sufixes) {
  53. CopyFile(destFile, srcFile)
  54. }
  55. }
  56. func DoDecodeFile(srcFile string, destFile string, password string, sign string, wg *sync.WaitGroup, sufixes []string) {
  57. defer wg.Done()
  58. if !DecodeFile(srcFile, destFile, password, sign, sufixes) {
  59. CopyFile(destFile, srcFile)
  60. }
  61. }
  62. func EncodeDir(srcPath string, destPath string, password string, sign string, wg *sync.WaitGroup, sufixes []string) {
  63. dir, err := os.ReadDir(srcPath)
  64. if err != nil {
  65. return
  66. }
  67. // 创建文件夹
  68. os.MkdirAll(destPath, 0777)
  69. for _, fi := range dir {
  70. if fi.IsDir() { // 忽略目录
  71. EncodeDir(srcPath+PthSep+fi.Name(), destPath+PthSep+fi.Name(), password, sign, wg, sufixes)
  72. } else {
  73. //fmt.Println(dirPth + PthSep + fi.Name())
  74. srcFile := srcPath + PthSep + fi.Name()
  75. destFile := destPath + PthSep + fi.Name()
  76. wg.Add(1)
  77. go DoEncodeFile(srcFile, destFile, password, sign, wg, sufixes)
  78. }
  79. }
  80. }
  81. func DecodeDir(srcPath string, destPath string, password string, sign string, wg *sync.WaitGroup, sufixes []string) {
  82. dir, err := os.ReadDir(srcPath)
  83. if err != nil {
  84. return
  85. }
  86. // 创建文件夹
  87. os.MkdirAll(destPath, 0777)
  88. for _, fi := range dir {
  89. if fi.IsDir() { // 忽略目录
  90. DecodeDir(srcPath+PthSep+fi.Name(), destPath+PthSep+fi.Name(), password, sign, wg, sufixes)
  91. } else {
  92. //fmt.Println(dirPth + PthSep + fi.Name())
  93. srcFile := srcPath + PthSep + fi.Name()
  94. destFile := destPath + PthSep + fi.Name()
  95. wg.Add(1)
  96. go DoDecodeFile(srcFile, destFile, password, sign, wg, sufixes)
  97. }
  98. }
  99. }
  100. func StartEncodeDir(srcPath string, destPath string, password string, sign string, sufixes []string) {
  101. var wg sync.WaitGroup
  102. EncodeDir(srcPath, destPath, password, sign, &wg, sufixes)
  103. wg.Wait()
  104. }
  105. func StartDecodeDir(srcPath string, destPath string, password string, sign string, sufixes []string) {
  106. var wg sync.WaitGroup
  107. DecodeDir(srcPath, destPath, password, sign, &wg, sufixes)
  108. wg.Wait()
  109. }