Skip to content

Commit

Permalink
⚡ Chore: unified content encryption function
Browse files Browse the repository at this point in the history
  • Loading branch information
Lruihao committed Aug 26, 2024
1 parent 91f3c7c commit e7dd4c8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 29 deletions.
12 changes: 2 additions & 10 deletions layouts/_default/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,8 @@ <h1 class="single-title animate__animated animate__pulse animate__faster">{{- ti

{{- /* Content */ -}}
{{- $content := dict "Content" .Content "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
{{- if $params.password -}}
{{- $saltLen := strings.RuneCount (trim $params.password " ") -}}
{{- $saltLen = cond (eq (mod $saltLen 2) 0) (add $saltLen 1) $saltLen -}}
{{- $base64EncodeContent := $content | base64Encode -}}
{{- $content = printf "%v%v%v"
(substr $base64EncodeContent 0 $saltLen)
(substr (sha256 $params.password) $saltLen)
(substr $base64EncodeContent $saltLen)
-}}
{{- end -}}
{{- /* Content Encryption */ -}}
{{- $content = dict "Content" $content "Password" $params.password | partial "function/content-encryption.html" | safeHTML -}}
<div class="content" id="content"
{{- with $params.password }} data-password="{{ hash.XxHash . }}"{{ end }}
{{- with $params.password }} data-content="{{ $content }}"{{ end -}}
Expand Down
14 changes: 14 additions & 0 deletions layouts/partials/function/content-encryption.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{- $content := .Content -}}

{{- if .Password -}}
{{- $saltLen := strings.RuneCount (trim .Password " ") -}}
{{- $saltLen = cond (eq (mod $saltLen 2) 0) (add $saltLen 1) $saltLen -}}
{{- $base64EncodeContent := $content | base64Encode -}}
{{- $content = printf "%v%v%v"
(substr $base64EncodeContent 0 $saltLen)
(substr (sha256 .Password) $saltLen)
(substr $base64EncodeContent $saltLen)
-}}
{{- end -}}

{{- return $content -}}
2 changes: 1 addition & 1 deletion layouts/partials/init/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- .Scratch.Set "version" "v0.3.10-466bb7b7" -}}
{{- .Scratch.Set "version" "v0.3.10-91f3c7c3" -}}
{{- .Scratch.Set "this" dict -}}

{{- partial "init/detection-env.html" . -}}
Expand Down
12 changes: 2 additions & 10 deletions layouts/posts/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,8 @@ <h1 class="single-title animate__animated animate__flipInX">

{{- /* Content */ -}}
{{- $content := dict "Content" .Content "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
{{- if $params.password -}}
{{- $saltLen := strings.RuneCount (trim $params.password " ") -}}
{{- $saltLen = cond (eq (mod $saltLen 2) 0) (add $saltLen 1) $saltLen -}}
{{- $base64EncodeContent := $content | base64Encode -}}
{{- $content = printf "%v%v%v"
(substr $base64EncodeContent 0 $saltLen)
(substr (sha256 $params.password) $saltLen)
(substr $base64EncodeContent $saltLen)
-}}
{{- end -}}
{{- /* Content Encryption */ -}}
{{- $content = dict "Content" $content "Password" $params.password | partial "function/content-encryption.html" | safeHTML -}}
<div class="content" id="content"
{{- with $params.endFlag }} data-end-flag="{{ . }}"{{ end }}
{{- with $params.password }} data-password="{{ xxhash . }}"{{ end }}
Expand Down
11 changes: 3 additions & 8 deletions layouts/shortcodes/fixit-encryptor.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@
{{- end -}}

{{- if $password -}}
{{- /* Content */ -}}
{{- $content := .Inner | .Page.RenderString -}}
{{- $saltLen := strings.RuneCount (trim $password "") -}}
{{- $saltLen = cond (eq (mod $saltLen 2) 0) (add $saltLen 1) $saltLen -}}
{{- $base64EncodeContent := $content | base64Encode -}}
{{- $content = printf "%v%v%v"
(substr $base64EncodeContent 0 $saltLen)
(substr (sha256 $password) $saltLen)
(substr $base64EncodeContent $saltLen)
-}}
{{- /* Content Encryption */ -}}
{{- $content = dict "Content" $content "Password" $password | partial "function/content-encryption.html" | safeHTML -}}

<fixit-encryptor class="fixit-encryptor-shortcode">
<div class="fixit-decryptor-container">
Expand Down

0 comments on commit e7dd4c8

Please sign in to comment.