+ Rename getReversedOperator and genRandOperator

+ Small refactoring
pull/90/head
Pagran 5 years ago
parent e59e0b8b36
commit 6ae6b50308

@ -53,9 +53,9 @@ func genRandIntSlice(max, count int) []int {
return indexes
}
var allOperators = []token.Token{token.XOR, token.ADD, token.SUB}
var allOperators = [...]token.Token{token.XOR, token.ADD, token.SUB}
func genRandOperator() token.Token {
func randOperator() token.Token {
return allOperators[mathrand.Intn(len(allOperators))]
}
@ -68,15 +68,12 @@ func evalOperator(t token.Token, x, y byte) byte {
case token.SUB:
return x - y
default:
panic("unknown operator")
panic(fmt.Sprintf("unknown operator: %s", t))
}
}
func getReversedOperator(t token.Token, x, y ast.Expr) *ast.BinaryExpr {
expr := &ast.BinaryExpr{
X: x,
Y: y,
}
func operatorToReversedBinaryExpr(t token.Token, x, y ast.Expr) *ast.BinaryExpr {
expr := &ast.BinaryExpr{X: x, Y: y}
switch t {
case token.XOR:
@ -86,7 +83,7 @@ func getReversedOperator(t token.Token, x, y ast.Expr) *ast.BinaryExpr {
case token.SUB:
expr.Op = token.ADD
default:
panic("unknown operator")
panic(fmt.Sprintf("unknown operator: %s", t))
}
return expr

@ -76,7 +76,7 @@ func (x split) obfuscate(data []byte) *ast.BlockStmt {
chunks = splitIntoRandomChunks(data)
}
decryptOp := genRandOperator()
decryptOp := randOperator()
// Generate indexes for cases chunk count + 1 decrypt case + 1 exit case
indexes := mathrand.Perm(len(chunks) + 2)
@ -107,7 +107,7 @@ func (x split) obfuscate(data []byte) *ast.BlockStmt {
Lhs: []ast.Expr{ah.IndexExpr("data", ah.Ident("y"))},
Tok: token.ASSIGN,
Rhs: []ast.Expr{
getReversedOperator(decryptOp, ah.IndexExpr("data", ah.Ident("y")), ah.CallExpr(ah.Ident("byte"), &ast.BinaryExpr{
operatorToReversedBinaryExpr(decryptOp, ah.IndexExpr("data", ah.Ident("y")), ah.CallExpr(ah.Ident("byte"), &ast.BinaryExpr{
X: ah.Ident("decryptKey"),
Op: token.XOR,
Y: ah.Ident("y"),

@ -59,7 +59,7 @@ func (x swap) obfuscate(data []byte) *ast.BlockStmt {
swapCount := generateSwapCount(len(data))
shiftKey := byte(mathrand.Intn(math.MaxUint8))
localKeyOp := genRandOperator()
localKeyOp := randOperator()
positions := genRandIntSlice(len(data), swapCount)
for i := len(positions) - 2; i >= 0; i -= 2 {
@ -129,12 +129,12 @@ func (x swap) obfuscate(data []byte) *ast.BlockStmt {
},
Tok: token.ASSIGN,
Rhs: []ast.Expr{
getReversedOperator(localKeyOp, ah.IndexExpr("data", ah.IndexExpr("positions", &ast.BinaryExpr{
operatorToReversedBinaryExpr(localKeyOp, ah.IndexExpr("data", ah.IndexExpr("positions", &ast.BinaryExpr{
X: ah.Ident("i"),
Op: token.ADD,
Y: ah.IntLit(1),
})), ah.Ident("localKey")),
getReversedOperator(localKeyOp, ah.IndexExpr("data", ah.IndexExpr("positions", ah.Ident("i"))), ah.Ident("localKey")),
operatorToReversedBinaryExpr(localKeyOp, ah.IndexExpr("data", ah.IndexExpr("positions", ah.Ident("i"))), ah.Ident("localKey")),
},
},
),

@ -16,7 +16,7 @@ func (x xor) obfuscate(data []byte) *ast.BlockStmt {
key := make([]byte, len(data))
genRandBytes(key)
op := genRandOperator()
op := randOperator()
for i, b := range key {
data[i] = evalOperator(op, data[i], b)
}
@ -41,7 +41,7 @@ func (x xor) obfuscate(data []byte) *ast.BlockStmt {
&ast.AssignStmt{
Lhs: []ast.Expr{ah.IndexExpr("data", ah.Ident("i"))},
Tok: token.ASSIGN,
Rhs: []ast.Expr{getReversedOperator(op, ah.IndexExpr("data", ah.Ident("i")), ah.Ident("b"))},
Rhs: []ast.Expr{operatorToReversedBinaryExpr(op, ah.IndexExpr("data", ah.Ident("i")), ah.Ident("b"))},
},
}},
},

@ -19,7 +19,7 @@ func (x xorSeed) obfuscate(data []byte) *ast.BlockStmt {
seed := preSeed[0]
originalSeed := seed
op := genRandOperator()
op := randOperator()
var callExpr *ast.CallExpr
for i, b := range data {
@ -97,7 +97,7 @@ func (x xorSeed) obfuscate(data []byte) *ast.BlockStmt {
Lhs: []ast.Expr{ah.Ident("data")},
Tok: token.ASSIGN,
Rhs: []ast.Expr{
ah.CallExpr(ah.Ident("append"), ah.Ident("data"), getReversedOperator(op, ah.Ident("x"), ah.Ident("seed"))),
ah.CallExpr(ah.Ident("append"), ah.Ident("data"), operatorToReversedBinaryExpr(op, ah.Ident("x"), ah.Ident("seed"))),
},
},
&ast.AssignStmt{

@ -20,7 +20,7 @@ func (x xorShuffle) obfuscate(data []byte) *ast.BlockStmt {
fullData := make([]byte, len(data)+len(key))
operators := make([]token.Token, len(fullData))
for i := range operators {
operators[i] = genRandOperator()
operators[i] = randOperator()
}
for i, b := range key {
@ -36,7 +36,7 @@ func (x xorShuffle) obfuscate(data []byte) *ast.BlockStmt {
args := []ast.Expr{ah.Ident("data")}
for i := range data {
args = append(args, getReversedOperator(
args = append(args, operatorToReversedBinaryExpr(
operators[i],
ah.IndexExpr("fullData", ah.IntLit(shuffledIdxs[i])),
ah.IndexExpr("fullData", ah.IntLit(shuffledIdxs[len(data)+i]))),

Loading…
Cancel
Save